20+個Selenium自動化測試常用指令範例 (Java)

20+個Selenium自動化測試常用指令範例

這篇文章主要彙整Selenium自動化測試常用指令 (Java範例)

透過 20個小範例說明每一種自動化測試處理的狀況。

 

啟動FireFox瀏覽器

WebDriver driver = new FirefoxDriver();

瀏覽網站

driver.get("http://www.google.com");


點擊網頁元件

driver.findElement(By.id("myButton")).click();

取得元件的顯示文字

String msg = driver.findElement(By.tagName("select")).getText();

 

輸入文字

driver.findElement(By.name("fname")).sendKeys("This is my input message");

 

Implicit Wait

為自動化測試程式設定預設 timeout 值,例如15秒

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

特定條件下的等待

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@name='q']"), "Text Here"));

取得網頁 title

driver.getTitle();

取得當下 URL

driver.getCurrentUrl();

 

執行 JavaScript

選取下拉式選單

Select mydrpdwn = new Select(driver.findElement(By.id("language")));
mydrpdwn.selectByVisibleText("Japan");

 

 

ListBox選取

 

Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.selectByIndex(0)

取消選取

Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectByVisibleText("Russia");
 
  • Deselect by Value
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectByValue("Mexico");

  • Deselect by Index
It will deselect option by Index = 5 from list box.

 

  • Deselect All

 

Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));

listbox.deselectAll();

 

是否可多選?

  • isMultiple()


網頁瀏覽

driver.navigate().to(“http://www.google.com/”);

driver.navigate().back();

driver.navigate().forward();

 

驗證元件的存在與否

Boolean iselementpresent = driver.findElements(By.xpath("//input[@id='text']")).size()!= 0;

 

截取螢幕畫面

 

 

滑鼠的 Mouse Hover 選單操作

 

Actions actions = new Actions(driver);

WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div"));

actions.moveToElement(moveonmenu);

actions.perform();

 

處理新視窗與切換

Set<String> AllWindowHandles = driver.getWindowHandles();

String window1 = (String) AllWindowHandles.toArray()[0];
String window2 = (String) AllWindowHandles.toArray()[1];

driver.switchTo().window(window2);

 

網頁元件是否可以運作

boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();

System.out.print(fname);

透過 JavaScript改變網頁元件屬性

JavascriptExecutor javascript = (JavascriptExecutor) driver;
String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');"; javascript.executeScript(todisable);

String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');"; javascript.executeScript(toenable);

 

Enter的替代方案submit

可以直接透過 submit的方式取代 Enter或是確定的按鈕 click
driver.findElement(By.xpath("//input[@name='Company']")).submit();

TestNG驗證的基本使用

  • Assert.assertEquals(actual, expected);
  • Assert.assertTrue(condition);

 

Alert的處理

取得Alert視窗的訊息文字

String myalert = driver.switchTo().alert().getText();

 

按 Alert視窗的 OK

driver.switchTo().alert().accept();

 

按 Alert視窗的 Cancel

driver.switchTo().alert().dismiss();

Alert視窗上輸入文字

driver.switchTo().alert().sendKeys("This Is my Message ");

 

Leave a Reply

Your email address will not be published.