當前位置:編程學習大全網 - 源碼下載 - 熟練掌握Selenium工具核心功能總綱(二)

熟練掌握Selenium工具核心功能總綱(二)

舉壹反三:

那麽下拉框如何定位頁面元素呢?dom示例:

那麽定位去操作它的方法,也應該同多選框操作壹直:

@Test

void test_select() throws InterruptedException {

WebElement element = driver.findElement(By.id("fruitselect"));

Select select = new Select(element);

// 下面根據不同方法選擇下拉框的值

Thread.sleep(2000);

select.selectByIndex(2);

Thread.sleep(2000);

select.selectByValue("orange");

Thread.sleep(2000);

select.selectByVisibleText(" 蘋果 ");

// 遍歷集合打印下拉框的所有值

List options = select.getOptions();

for (WebElement option : options) {

System.out.println("輸出單選值:"+option.getText());

}

}

8、掌握alert、confirm、prompt三種彈框處理;這三種彈窗的處理方式不出意外都是Alert類來處理。

9、 selenium 三種等待方式的原理及使用場景:強制等待、隱式等待、顯示等待;時間同步機制。

10、selenium等待條件,isEnabled()isSelected()源碼分析:

比如在某些場景中,需要等待元素出現後才能操作它,這時需要的是等待,至於是哪種等待呢?需要結合實際場景來分析:

// 下面這個操作是先找元素,是否現實,這裏有壹個條件即使是隱藏元素,它還是存在dom中,而有些隱藏是不壹定會存在dom,那麽查找元素是會報異常:NoSuchElementException

@Test

public void test_display() throws InterruptedException {

WebElement el = driver.findElement(By.id("displayed-text"));

System.out.println("判斷輸入框是否顯示:" + el.isDisplayed());

if (el.isDisplayed()) {

el.sendKeys("displayed");

System.out.println("點擊隱藏按鈕,元素被隱藏");

driver.findElement(By.id("hide-textbox")).click();

}

Thread.sleep(3000);

WebElement ele = driver.findElement(By.id("displayed-text"));

// 因為知道上面的操作是默認顯示,並且輸入值,if判斷為true表示顯示,然後輸入文本內容並點擊隱藏

if (!ele.isDisplayed()) {

// 如果是隱藏的,下面操作顯示並輸入內容

driver.findElement(By.id("show-textbox")).click();

System.out.println("點擊顯示按鈕,元素被顯示");

ele.sendKeys("+再次打開");

}

Thread.sleep(3000);

// 然後再次隱藏

driver.findElement(By.id("hide-textbox")).click();

}

tips:元素的操作,判斷狀態:isEnabled,判斷顯示:isDisplayed,判斷是否可選:isSelected

11、鼠標操作:單擊、雙擊、右擊、鍵盤輸入及組合鍵的應用;前提是前端開發支持按鍵操作。

web 自動化中,所有鍵盤操作不壹定是支持的,需要前端開發支持。

import org.openqa.selenium.Keys;// 這個是selenium框架的關鍵類

// 發送單個按鍵操作,element的sendKeys()方法即可

findElement(By.id("kw")).sendKeys(Keys.ENTER);

// 如果是組合鍵,例如control+a

String select=Keys.chord(Keys.CONTROL,"a");

findElement(By.id("kw")).sendKeys(select);

// 使用Actions類處理按鍵操作

import org.openqa.selenium.interactions.Actions;

Actions action = new Actions(driver);

action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

12、需要掌握執行JavaScript的方法和技巧,JavascriptExecutor類:

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.chrome.ChromeDriver;

System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");

driver = new ChromeDriver();

// 下行強制轉換

JavascriptExecutor js=(JavascriptExecutor) driver;

13、屏幕截圖:實現截屏及日期格式保存圖片

思考:會寫在哪個位置?

通常使用TestNG/junit測試框架,會使用@After註解,也就是說在每個 測試 用例執行完才截圖,而不是壹個測試類執行完才截圖,那麽就需要對執行的結果進行斷言,錯誤才截圖,而不是隨意截圖。

優先封裝壹個生成文件名的方法:

/**

* 獲取隨機字符串作為文件名的長度

* @param length

* @return

*/

public static String getRandomString(int length) {

StringBuilder sb = new StringBuilder();

String characters = "1234567890qwertyuioplkjhgfdsazxcvbnmPOIUYTREWQASDFGHJKLMNBVCXZ";

for (int i = 0; i < length; i++) {

int index = (int) (Math.random() * characters.length());

sb.append(index);

}

return sb.toString();

}

  • 上一篇:如何安裝配置Python的GUI框架PySide?
  • 下一篇:入職三月的工作總結
  • copyright 2024編程學習大全網