1.怎么 判斷元素是否存在?
判斷元素是否存在和是否出現不同, 判斷是否存在意味着如果這個元素壓根就不存在, 就會拋出NoSuchElementException
這樣就可以使用try catch,如果catch到NoSuchElementException 就返回false
2.如何判斷元素是否出現?
判斷元素是否出現,存在兩種情況,一種是該元素壓根就沒有,自然不會出現;另外一種是有這樣的元素,但是是hidden狀態
可以通過先判斷是否存在,如果不存在返回false;如果存在再去判斷是否displayed
3. 怎樣選擇下拉菜單的元素
下拉菜單分兩種,一種是直接使用select標簽的,這種情況可以直接使用selenium API
參考:http://www.cnblogs.com/tobecrazy/p/4570494.html
WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(selector); 選擇select的option有以下三種方法 selectByIndex(int index) 通過index selectByVisibleText(String text) 通過匹配到的可見字符 selectByValue(String value) 通過匹配到標簽里的value
第二種下拉菜單不是通過select實現的,可以通過JS進行操作
類似這樣的:http://css3menu.com/ (關於怎么使用DevTools 請自行百度,不解釋)
那么這樣的菜單該怎么去選取?
可以收工演示一下,第一步鼠標移動到how to use,此時菜單出現;第二步,點擊Technical Question
要實現第一步,使用selenium 的Action clickAndHold,接着就可findByElement進行操作
WebElement menu = driver.findElement(By.xpath("//span[.='How to Use']")); Actions action = new Actions(driver); action.clickAndHold(menu).build().perform(); WebElement technicalQuestion = driver.findElement(By.xpath( "//ul[@id='css3menu_top']/li[position()=3]/div[@class='submenu']/div[@class='column']//a[contains(text(),'Technical')]")); technicalQuestion.click();
4. selenium 有幾種定位方式,你最常用哪種, 為什么?
selenium有八種定位方式,和name有關的3個ByName,ByClassName,ByTagName
和link有關的2個ByLinkText,ByPartialLinkText
和id有關的1個ById
剩下兩個全能的ByXpath和ByCssSelector
我最常用的事ByXpath(或CssSelector)因為很多情況下,html標簽的屬性不夠規范,無法通過單一的屬性定位,這個時候就只能使用xpath可以去重實現定位唯一element
事實上定位最快的應當屬於ById,因為id是唯一的,然而大多數開發並沒有設置id
5.去哪網面試題Java實現
一、 UI自動化測試
1、 Qunar機票搜索場景
1) 訪問Qunar機票首頁http://flight.qunar.com,選擇“單程”,輸入出發、到達城市,選擇today+7日后的日期,點“搜索”,跳轉到機票單程搜索列表頁。
2) 在列表頁停留1分鍾,至到頁面上出現“搜索結束”。
3) 如果出現航班列表,對於出現“每段航班均需繳納稅費”的行隨機點選“訂票”按鈕,在展開的列表中會出現“第一程”、 “第二程”;對於沒有出現“每段航班均需繳納稅費”的行隨機點選“訂票”按鈕,在展開的列表底部中會出現“報價范圍”
4) 如果不出現航班列表,則頁面會出現“該航線當前無可售航班”
參考我的blog, http://www.cnblogs.com/tobecrazy/p/4752684.html
去哪兒網輸入框三種輸入方式(selenium webdriver 干貨)
在機票預定的頁面,輸入出發城市和到達城市輸入框的時候, 發現直接使用sendkeys不好使,
大部分情況出現輸入某城市后沒有輸入進去, 經過幾天的研究,發現可以采取三種方式:
1. 先點擊輸入框,待彈出 城市選擇框之后,點擊相應的城市
2. 緩慢輸入城市的縮略字母或者城市的名字的部分,會顯示出待選城市的下拉列表,進而從下拉列表中選擇相應的城市.
3. 直接執行 js腳本對input的value設置為想要的值
首先說一下第三種方式:
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].value=\"北京\"", from_inpox);
執行效果最好,
22:35:34.885 INFO - Executing: [execute script: arguments[0].value="北京", [[[Ch
romeDriver: chrome on XP (6452a4a961be7bffa2af9d1b63f3d111)] -> xpath: //div[@id
='js_flighttype_tab_domestic']//input[@name='fromCity']]]])
如上圖所演示,兩種方式均是用戶真實行為。
采取第一種方式:
- 首先定位到輸入框
- 點擊輸入框
- 從彈出的熱門城市框中點擊所需要的城市
WebElement from_inpox = driver .findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']")); Actions actions = new Actions(driver); actions.moveToElement(from_inpox).click().perform(); driver.findElement(By .xpath("//div[@data-panel='domesticfrom-flight-hotcity-from']//a[@class='js-hotcitylist' and text()='西安']")) .click();
這里我並沒有直接使用click, 而是使用Actions,原因是我在對到達城市操作時,發現經常報element can't be clicked這樣的錯誤,
大意是,當要點擊到達城市輸入框,其實是被上層的元素遮擋,沒法使用click方法,但是可以使用Actions的moveToElement方法之后可以click
或者采取滾動到該元素,調用JS
JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("arguments[0].scrollIntoView()",element);
之后就可進行click操作.
如果使用第二種方法,就會遇到一個很大的問題:
如何定位到JS生成的下拉列表的城市?Firebug定位之前列表就消失!
看上去很難哈,反復嘗試無所成, 最后突然想起既然是JS生成的,何不使用瀏覽器的JS debug功能,設置斷點一步一步
果不其然,葯到病除。nice job~
思路有了,跟我一起做,點開firebug ,切換到“腳本”界面,首先在輸入框輸入單字母s,待彈出下拉列表后,單擊左側的插入斷點操作
你會發現該下拉框被凍結,不錯呦,之后切換到html界面進行定位。
不光是去哪網,像百度輸入框也可以采取這樣的辦法,JS設置斷點,js的彈出框,彈出菜單就會凍結.
接下來我的輸入就是選擇下拉菜單中所需城市:
from_inpox.clear(); from_inpox.sendKeys("BJ"); Thread.sleep(8000); By bj=new By.ByXPath("//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'北京')]"); if(isElementPresent(driver,bj,20)) { driver.findElement(bj).click(); }
所要注意的是,下拉菜單中未必彈出那么快,需要做一次等待,在選擇下拉菜單的時候需要做一次判斷,當然這個判斷方法是使用WebDriverWait
/** * @author Young * @param driver * @param by * @param timeOut * @return */ public static boolean isElementPresent(WebDriver driver, final By by, int timeOut) { WebDriverWait wait = new WebDriverWait(driver, timeOut); boolean isPresent = false; isPresent = wait.until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver d) { return d.findElement(by); } }).isDisplayed(); return isPresent; }
依然不夠完美,為什么這么說,如果元素沒有出現,並不是返回的false而是直接拋異常,並不是期望的,所以修改為findElements
如果找不到,返回List長度必然為0,進而返回false而不是拋出異常
/** * @author Young * @param driver * @param by * @param timeOut * @return * @throws InterruptedException */ public static boolean isElementPresent(WebDriver driver, final By by, int timeOut) throws InterruptedException { boolean isPresent = false; Thread.sleep(timeOut * 1000); List<WebElement> we = driver.findElements(by); if (we.size() != 0) { isPresent = true; } return isPresent; }
測試步驟:
1.選擇出發城市-> 北京
到達城市->上海
選擇今天之后的七天
點擊search button
2.選擇某帶“每段航班均需繳納稅費” 的訂單
public static void main(String[] args) throws InterruptedException {
WebDriver driver = DriverFactory.getChromeDriver();
driver.get("http://flight.qunar.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebElement from_inpox = driver
.findElement(By
.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']"));
WebElement to_inpox = driver
.findElement(By
.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']"));
WebElement from_date = driver
.findElement(By
.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']"));
WebElement sigleWayCheckBox = driver
.findElement(By
.xpath("//div[@id='js_flighttype_tab_domestic']//input[@class='inp_chk js-searchtype-oneway']"));
if (!sigleWayCheckBox.isSelected()) {
sigleWayCheckBox.click();
}
from_inpox.clear();
from_inpox.sendKeys("BJ");
Thread.sleep(8000);
By bj = new By.ByXPath(
"//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'北京')]");
if (isElementPresent(driver, bj, 20)) {
driver.findElement(bj).click();
}
to_inpox.clear();
to_inpox.sendKeys("SH");
Thread.sleep(8000);
By sh = new By.ByXPath(
"//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'上海')]");
if (isElementPresent(driver, sh, 20)) {
driver.findElement(sh).click();
}
// Actions actions = new Actions(driver);
// actions.moveToElement(from_inpox).click().perform();
// driver.findElement(
// By.xpath("//div[@data-panel='domesticfrom-flight-hotcity-from']//a[@class='js-hotcitylist' and text()='西安']"))
// .click();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
// actions.moveToElement(to_inpox).click().perform();
// driver.findElement(
// By.xpath("//div[@data-panel='domesticto-flight-hotcity-to']//a[@class='js-hotcitylist' and text()='北京']"))
// .click();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
from_date.clear();
from_date.sendKeys(getDateAfterToday(7));
WebElement search = driver
.findElement(By
.xpath("//div[@id='js_flighttype_tab_domestic']//button[@class='btn_search']"));
search.submit();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebElement page2 = driver.findElement(By
.xpath("//div[@id='hdivPager']/a[@value='2']"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView()", page2);
page2.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.findElement(
By.xpath("(//div[@class='avt_trans']//p[contains(text(),'每段航班均需繳納稅費')]/ancestor::div//div[@class='a_booking']/a)[3]"))
.click();
driver.findElement(
By.xpath("//div[@id='flightbarXI883']//div[@class='t_bk']/a"))
.click();
}
public static String getDateAfterToday(int dateAfterToday) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, +dateAfterToday);
System.out.println(cal.getTime().toString());
Date date = cal.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(date));
return df.format(date);
}
/**
* @author Young
* @param driver
* @param by
* @param timeOut
* @return
* @throws InterruptedException
*/
public static boolean isElementPresent(WebDriver driver, final By by,
int timeOut) throws InterruptedException {
boolean isPresent = false;
Thread.sleep(timeOut * 1000);
List<WebElement> we = driver.findElements(by);
if (we.size() != 0) {
isPresent = true;
}
return isPresent;
}
效果如下:
6. 如何去定位頁面上動態加載的元素?
觸發動態事件事件,進而findElemnt
如果是動態菜單,需要一級一級find
7.如何去定位屬性動態變化的元素?
屬性動態變化是指該element沒有固定的屬性值,所以只能通過相對位置定位
比如通過xpath的軸, parent/following-sibling/precent-sibling等
另外也可以嘗試findbyelements遍歷
8.怎么提高selenium腳本的自動化執行效率?
- 優化測試用例,盡可不使用 sleep,減少使用ImplicitlyWait
,而使用selenium的wait/FluentWait,這樣可以優化等待時間 - 使用selenium grid,通過testng實現並發執行
- 針對一些不穩定的動態控件通過JS實現操作
- 重載testng的listener實現retry機制,提高測試用例成功率
- 減少使用IE的driver,IE執行效率太低!!!
9. webdriver 的原理是什么?
結合上次研究的selenium webdriver potocol ,自己寫http request調用remote driver代替selenium API
selenium web driver Json protocol 相關請看 http://www.cnblogs.com/tobecrazy/p/5020741.html
我這里使用的是Gson 和 httpclient
首先,起一個remote sever
java -Dwebdriver.ie.driver="IEDriverServer.exe" -Dwebdriver.chrome.driver="chromedriver.exe" -jar selenium-server-standalone-2.48.0.jar
這里要用到httpclient的Post 和delete method
創建一個httpclient對象
HttpClient httpClient = HttpClients.createDefault();
創建一個post請求
JsonObject setCapability = new JsonObject(); setCapability.addProperty("browserName","firefox"); JsonObject capability = new JsonObject(); capability.add("desiredCapabilities",setCapability); HttpPost httpPost = new HttpPost(base);
創建一個delete 請求
url = base + sessionId ; HttpDelete httpDelete = new HttpDelete(url);
從respose 中獲取session ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
HttpResponse response = httpClient.execute(httpPost);
try
{
HttpEntity entity = response.getEntity();
if
(entity !=
null
) {
System.out.println(
"Response content length: "
+ entity.getContentLength());
String resultEntity = EntityUtils.toString(entity);
System.out.println(
"Response content: "
+ resultEntity);
JsonObject result=
new
JsonParser().parse(resultEntity).getAsJsonObject();
JsonElement sessionIdJson = result.get(
"sessionId"
);
if
(!sessionIdJson.isJsonNull())
sessionId =sessionIdJson.getAsString();
JsonElement valueJson = result.get(
"value"
);
if
(!valueJson.isJsonNull())
{
JsonObject tm=valueJson.getAsJsonObject();
JsonElement elementIdJson = tm.get(
"ELEMENT"
);
if
(elementIdJson!=
null
)
elementId=elementIdJson.getAsString();
}
}
}
finally
{
((Closeable) response).close();
}
|
全部代碼如下:
import java.io.Closeable; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class webDriverJson { private static String base = "http://127.0.0.1:4444/wd/hub/session/"; private static String elementId; static String sessionId = ""; public static void main(String[] args) throws Exception, IOException { HttpClient httpClient = HttpClients.createDefault(); JsonObject setCapability = new JsonObject(); setCapability.addProperty("browserName","firefox"); JsonObject capability = new JsonObject(); capability.add("desiredCapabilities",setCapability); HttpPost httpPost = new HttpPost(base); //create session postExecutor(httpClient, httpPost, capability); String url = base + sessionId + "/url"; httpPost = new HttpPost(url); JsonObject getUrl = new JsonObject(); getUrl.addProperty("url", "http://www.baidu.com"); postExecutor(httpClient, httpPost, getUrl); //find input box url = base + sessionId + "/element"; httpPost = new HttpPost(url); JsonObject findElement = new JsonObject(); findElement.addProperty("using", "id"); findElement.addProperty("value", "kw"); postExecutor(httpClient, httpPost, findElement); System.out.println(elementId); url = base + sessionId + "/element/"+elementId+"/value"; httpPost = new HttpPost(url); JsonObject typeElement = new JsonObject(); String json = "{\"value\":[\"webdriver\"]}"; JsonParser jp = new JsonParser(); typeElement = (JsonObject) jp.parse(json); postExecutor(httpClient, httpPost, typeElement); //find search button url = base + sessionId + "/element"; httpPost = new HttpPost(url); JsonObject findSearchButton = new JsonObject(); findSearchButton.addProperty("using", "id"); findSearchButton.addProperty("value", "su"); postExecutor(httpClient, httpPost, findSearchButton); System.out.println(elementId); url = base + sessionId + "/element/"+elementId+"/click"; httpPost = new HttpPost(url); postExecutor(httpClient, httpPost,null); //delete session url = base + sessionId ; HttpDelete httpDelete = new HttpDelete(url); deleteExecutor(httpClient, httpDelete); } /** * @author Young * @param httpClient * @param httpPost * @param jo * @throws UnsupportedEncodingException * @throws IOException * @throws ClientProtocolException */ public static void postExecutor(HttpClient httpClient, HttpPost httpPost, JsonObject jo) throws UnsupportedEncodingException, IOException, ClientProtocolException { if(jo!=null) { StringEntity input = new StringEntity(jo.toString()); input.setContentEncoding("UTF-8"); input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(input); } HttpResponse response = httpClient.execute(httpPost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); String resultEntity = EntityUtils.toString(entity); System.out.println("Response content: " + resultEntity); JsonObject result= new JsonParser().parse(resultEntity).getAsJsonObject(); JsonElement sessionIdJson = result.get("sessionId"); if(!sessionIdJson.isJsonNull()) sessionId =sessionIdJson.getAsString(); JsonElement valueJson = result.get("value"); if(!valueJson.isJsonNull()) { JsonObject tm=valueJson.getAsJsonObject(); JsonElement elementIdJson = tm.get("ELEMENT"); if(elementIdJson!=null) elementId=elementIdJson.getAsString(); } } } finally { ((Closeable) response).close(); } } /** * @author Young * @param httpClient * @param delete * @throws UnsupportedEncodingException * @throws IOException * @throws ClientProtocolException */ public static void deleteExecutor(HttpClient httpClient, HttpDelete delete) throws UnsupportedEncodingException, IOException, ClientProtocolException { HttpResponse response = httpClient.execute(delete); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); String resultEntity = EntityUtils.toString(entity); System.out.println("Response content: " + resultEntity); JsonObject result= new JsonParser().parse(resultEntity).getAsJsonObject(); JsonElement sessionIdJson = result.get("sessionId"); if(!sessionIdJson.isJsonNull()) sessionId =sessionIdJson.getAsString(); JsonElement valueJson = result.get("value"); if(!valueJson.isJsonNull()) { JsonObject tm=valueJson.getAsJsonObject(); JsonElement elementIdJson = tm.get("ELEMENT"); if(elementIdJson!=null) elementId=elementIdJson.getAsString(); } } } finally { ((Closeable) response).close(); } } }
運行效果:
了解selenium 原理究竟有什么意義?
大多數人都會使用selenium去做自動化,但是不是每個人都了解selenium的原理,如果能掌握selenium原理
可以改造selenium API,使用webdriver protocol去做一些能夠完善自動化測試框架的事情。、
比如,也許你在selenium自動化過程中會遇到get打開頁面打不開,為了保證你腳本的健壯性,這時候你可以加入一段httprequest去獲取
response的的關鍵值判斷,如果不是2開頭的可以設置refresh,再比如需要做一些准備性工作,比如環境配置也可以使用
參考:http://www.cnblogs.com/tobecrazy/p/5034408.html
10. selenium中如何保證操作元素的成功率?也就是說如何保證我點擊的元素一定是可以點擊的?
參考:http://www.cnblogs.com/tobecrazy/p/4817946.html
- 通過封裝find方法實現waitforEmelentPresent,這樣在對元素進行操作之前保證元素被找到,進而提高成功率
- 在對元素操作之前,比如click,如果該元素未display(非hidden),就需要先滾動到該元素,然后進行click操作
為啥使用滾動? 因為如果頁面沒有完全顯示,element如果是在下拉之后才能顯示出來,只能先滾動到該元素才能進行click,否則是不能click操作
1
2
3
|
JavascriptExecutor js=(JavascriptExecutor)driver;
// roll down and keep the element to the center of browser
js.executeScript(
"arguments[0].scrollIntoViewIfNeeded(true);"
, download);
|
selenium常用的js總結
1、 對input執行輸入
直接設置value屬性, 此方法主要應對輸入框自動補全以及readonly屬性的element,sendkeys不穩定
比如:
//inputbox is a WebElement JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].value=\"北京\"", from_inpox);
對此可以封裝一個typeQuick的方法
/** * @author Young * @param locator * @param values * @throws Exception */ protected void typeQuick(Locator locator, String values) throws Exception { WebElement e = findElement(driver, locator); log.info("type value is: " + values); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].value=\""+values+"\"", e);
去掉只讀屬性
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].removeAttribute(\"+"readonly"+\")", e);
2.對富文本框的操作
主要應對富文本框,可以封裝獲取富文本框內容和設置富文本路況內容的方法
JavascriptExecutor js = (JavascriptExecutor) driver; WebElement editor = driver.findElement(By.tagName("body")); js.executeScript( "arguments[0].innerHTML = '<h1>Selenium Test </h1>I love Selenium <br> this article Post By Selenium WebDriver<br><h2>Create By Young</h2>'", editor);
設置富文本框內容
/** * @author Young * @param locator * @param text */ protected void setRichTextBox(Locator locator, String text) { WebElement e = findElement(driver, locator); log.info("type value is: " + text); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].innerHTML = \"" + text + "\"", e); }
獲取富文本框內容:
/** * @author Young * @param locator * @param text * @return */ protected String getRichTextBox(Locator locator, String text) { WebElement e = findElement(driver, locator); log.info("type value is: " + text); JavascriptExecutor js = (JavascriptExecutor) driver; String result=(String) js.executeScript("var result=arguments[0].innerHTML;return result", e); return result; }
3. 滾動到指定位置
為啥使用滾動? 因為如果頁面沒有完全顯示,element如果是在下拉之后才能顯示出來,只能先滾動到該元素才能進行click,否則是不能click操作
1
2
3
|
JavascriptExecutor js=(JavascriptExecutor)driver;
// roll down and keep the element to the center of browser
js.executeScript(
"arguments[0].scrollIntoViewIfNeeded(true);"
, download);
|
可以封裝滾動到元素的方法的
/** * @author Young * @param locator */ protected void scrollToElement(Locator locator) { WebElement e = findElement(driver, locator); log.info("scroll view element"); JavascriptExecutor js = (JavascriptExecutor) driver; // roll down and keep the element to the center of browser js.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", e); }
4. 觸發event,Dom event 事件的封裝
/** * For DOM Event * @author Young * @param locator * @param event * please refer to: http://www.w3school.com.cn/jsref/dom_obj_event.asp * */ public void DOMEvent(Locator locator,String event) { JavascriptExecutor jse=((JavascriptExecutor)driver); String js="var event;if (document.createEvent){event = document.createEvent(\"HTMLEvents\");event.initEvent(\""+event+"\", true, false);arguments[0].dispatchEvent(event);} else {arguments[0].fireEvent(\"on"+event+"\")}"; jse.executeScript(js, findElement(driver,locator)) ; }
五、獲取元素屬性
window.getComputedStyle(document.getElementById("su"),null).getPropertyValue("background")
六 、獲取頁面加載狀態
document.readyState
11. 什么PO模式,什么是page factory?
網上有很多答案,都不全面
PO模式是page object model的縮寫,顧名思義, 是一種設計模式,實現腳本的page和真實的網站頁面Map起來,一一對應起來。這樣能測試框架更容易維護。 比如一個登陸頁面,使用PO模式后,會創建一個LoginPage的class,該class會定義用戶名輸入框,密碼輸入框,登陸按鈕的webElenent
針對相應的Element實現相應的方法,輸入框是用來輸入的,就需要創建輸入用戶名和輸入密碼的方法,這樣就和真實的頁面一致,所以這樣的設計理念就是PO模式。 而PageFactory隸屬PO模式,是用來初始化每個PO模式實現的Page Class,初始化對象庫。