1. 程式人生 > >UI自動化測試之selenium(1)——selenium中的常用api

UI自動化測試之selenium(1)——selenium中的常用api

selenium api

目錄

1 對瀏覽器操作
1.1 用webdriver打開一個瀏覽器
1.2 最大化瀏覽器&關閉瀏覽器
1.3 設置瀏覽器窗口大小
1.4 打開測試頁面
1.5 處理瀏覽器彈出的新窗口
2 頁面元素定位
3 如何對頁面元素進行操作
3.1 WebElement相關方法
3.2 iFrame的處理
3.3 輸入框(text field or textarea)
3.4 下拉選擇框(Select)
3.5 單選項(Radio Button)
3.6 多選項(checkbox)
3.7 按鈕(button)
3.8 處理Alert
3.9 上傳文件
3.9.1 元素標簽是Input時上傳方式
3.9.2 通過操作桌面瀏覽窗口上傳

3.10 Selenium處理HTML5
3.10.1 處理Vedio
3.10.2 處理Canvas
3.11 表單(Form)
4 其他
4.1 等待元素加載
4.2 執行JS腳本
4.3 模擬鍵盤操作

1 對瀏覽器操作

返回
1.1 用webdriver打開一個瀏覽器
復制代碼

//打開firefox瀏覽器:
WebDriver driver = new FirefoxDriver();
//打開IE瀏覽器
WebDriver driver = new InternetExplorerDriver ();
//打開HtmlUnit瀏覽器
WebDriverdriver = new HtmlUnitDriver();
//打開chrome瀏覽器

WebDriverdriver = new ChromeDriver();

復制代碼
1.2 最大化瀏覽器&關閉瀏覽器

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.close();
driver.quit();

1.3 設置瀏覽器窗口大小
View Code
1.4 打開測試頁面

打開測試頁面
driver.get("http://www.baidu.com/");
driver.navigate().to("http://www.baidu.com/");

//navigate方法會產生1個Navigator對象,其封裝了與導航相關的一些方法,比如前進後退等

1.5 處理瀏覽器彈出的新窗口
View Code
2 頁面元素定位

返回

Webdriver提供下面兩種方法來定位頁面元素,參數是By對像,最常用是By.id和By.name查找。

findElement   定位某個元素,如果沒有找到元素會拋出異常:NoSuchElementException
findElements     定位一組元素

例如需要定位如下元素:

  <input class="input_class" type="text" name="passwd" id="passwd-id" />

復制代碼

//By.id
WebElement element = driver.findElement(By.id("passwd-id"));
//By.name
WebElement element = driver.findElement(By.name("passwd"));
//By.xpath
WebElement element =driver.findElement(By.xpath("//input[@id=‘passwd-id‘]"));
//By.className
WebElement element = driver.findElement(By.className("input_class"));
//By.cssSelector
WebElement element = driver.findElement(By.cssSelector(".input_class"));
//By.linkText
//通俗點就是精確查詢
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.linkText("百科"));
//By.partialLinkText:
//這個方法就是模糊查詢
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.partialLinkText("hao"));
//By.tagName
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);

復制代碼
3 如何對頁面元素進行操作

返回
3.1 WebElement相關方法
Method Summary
void clear() If this element is a text entry element, this will clear the value.
void click() Click this element.
WebElement findElement(By by) Find the first WebElement using the given method.
java.util.List<WebElement> findElement(By by) Find all elements within the current context using the given mechanism.
java.lang.String getAttribute(java.lang.String name) Get the value of a the given attribute of the element.
java.lang.String getCssValue(java.lang.String.propertyName) Get the value of a given CSS property.
Point GetLocation() Where on the page is the top left-hand corner of the rendered element?
Dimension getSize() What is the width and height of the rendered element?
java.lang.String getTagName() Get the tag name of this element.
java.lang.String getText() Get the visible (i.e. not hidden by CSS) innerText of this element, including
sub-elements, without any leading or trailing whitespace.
boolean isDisplayed() Is this element displayed or not? This method avoids the problem of having to parse an element‘s "style" attribute.
boolean isEnabled() Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
boolean isSelected() Determine whether or not this element is selected or not.
void sendKeys(java.lang.CharSequence... keysToSend) Use this method to simulate typing into an element, which may set its value.
void submit() If this current element is a form, or an element within a form, then this will be submitted to the remote server.
3.2 iFrame的處理

driver.switchTo().frame(“city_set_ifr”); //傳入的是iframe的ID
dr.switchTo().defaultContent(); //如果要返回到以前的默認content

3.3 輸入框(text field or textarea)

WebElement element = driver.findElement(By.id("passwd-id"));
element.sendKeys(“test”);//在輸入框中輸入內容:
element.clear();     //將輸入框清空
element.getText();   //獲取輸入框的文本內容:

3.4 下拉選擇框(Select)
復制代碼

Select select = new Select(driver.findElement(By.id("select")));
select.selectByVisibleText(“A”);
select.selectByValue(“1”);
select.deselectAll();
select.deselectByValue(“1”);
select.deselectByVisibleText(“A”);
select.getAllSelectedOptions();
select.getFirstSelectedOption();

復制代碼

示例:
View Code
3.5 單選項(Radio Button)

WebElement radio=driver.findElement(By.id("BookMode"));
radio.click();     //選擇某個單選項
radio.clear();     //清空某個單選項
radio.isSelected();  //判斷某個單選項是否已經被選擇

3.6 多選項(checkbox)

WebElement checkbox = driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();

3.7 按鈕(button)

WebElement btn= driver.findElement(By.id("save"));
btn.click();      //點擊按鈕
btn.isEnabled ();  //判斷按鈕是否enable

3.8 處理Alert

彈出對話框(Popup dialogs)

Alert alert = driver.switchTo().alert();
alert.accept();  //確定
alert.dismiss();  //取消
alert.getText(); //獲取文本

示例:
View Code
3.9 上傳文件
3.9.1 元素標簽是Input時上傳方式

Upload.html文件內容如下:

<body>
<input type="file" id="fileControl" value="選擇文件"/>
</body>

代碼如下:
View Code
3.9.2 通過操作桌面瀏覽窗口上傳

示例2 上傳文件
3.10 Selenium處理HTML5
3.10.1 處理Vedio

這就需要了解html5中vedio的相關方法了,可以參考http://www.w3school.com.cn/tags/html_ref_audio_video_dom.asp
View Code
3.10.2 處理Canvas
View Code
3.11 表單(Form)

//Form中的元素的操作和其它的元素操作一樣,對元素操作完成後對表單的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
//或
approve.submit();//只適合於表單的提交

4 其他

返回
4.1 等待元素加載

超時設置

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);     //識別元素時的超時時間
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  //頁面加載時的超時時間
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);  //異步腳本的超時時間

硬性等待  Thread.sleep(int sleeptime);
智能等待
設置等待頁面加載完畢

View Code
4.2 執行JS腳本

selenium常用的js總結

有時候我們需要JS腳本來輔助我們進行測試,比如我們用JS賦值或者用js執行點擊操作等。執行JS腳本比較適用某些元素不易點擊的情況下使用,比如網頁內容太長,當前窗口太長,想要點擊那些不在當前窗口可以看到元素可以用此方法。
View Code
4.3 模擬鍵盤操作

有時候有些元素不便點擊或者做其他的操作,這個時候可以借助selenium提供的Actions類,它可以模擬鼠標和鍵盤的一些操作,比如點擊鼠標右鍵,左鍵,移動鼠標等操作。對於這些操作,使用perform()方法進行執行。
復制代碼

private static void actionsTest(WebDriver driver)
        throws InterruptedException {
    // 設置等待頁面完全加載的時間是10秒,如果在10秒內加載完畢,剩余時間不在等待
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.get("https://www.baidu.com/");
    By inputBox = By.id("kw");
    By searchButton = By.id("su");
    // 智能等待元素加載出來
    intelligentWait(driver, 10, inputBox);
    // 智能等待元素加載出來
    intelligentWait(driver, 10, searchButton);
    // 實例化action對象
    Actions action = new Actions(driver);
    // 通過action模擬鍵盤輸入java關鍵字到 輸入框,只有使用了perform方法才會輸入進去
    action.sendKeys(driver.findElement(searchButton), "java").perform();
    // 鼠標模擬移動到搜索按鈕
    action.moveToElement(driver.findElement(searchButton)).perform();
    // 模擬點擊操作
    action.click().perform();
    Thread.sleep(2000);
}

UI自動化測試之selenium(1)——selenium中的常用api