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();

取得元件的顯示文字

[pastacode lang=”markup” message=”” highlight=”” provider=”manual”]

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

[/pastacode]

 

輸入文字

[pastacode lang=”markup” message=”” highlight=”” provider=”manual”]

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

[/pastacode]

 

Implicit Wait

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

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

特定條件下的等待

[pastacode lang=”markup” message=”” highlight=”” provider=”manual”]


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

[/pastacode]

取得網頁 title

driver.getTitle();

取得當下 URL

driver.getCurrentUrl();

 

執行 JavaScript

選取下拉式選單

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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

[/pastacode]

 

 

ListBox選取

 

[pastacode lang=”markup” message=”” highlight=”” provider=”manual”]


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

[/pastacode]

取消選取

[pastacode lang=”markup” message=”” highlight=”” provider=”manual”]

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

[/pastacode]

 
  • Deselect by Value

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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

[/pastacode]


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

 

  • Deselect All

 

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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

listbox.deselectAll();

[/pastacode]

 

是否可多選?

  • isMultiple()


網頁瀏覽

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

driver.navigate().back();

driver.navigate().forward();

 

驗證元件的存在與否

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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

[/pastacode]

 

截取螢幕畫面

 

 

滑鼠的 Mouse Hover 選單操作

 

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

Actions actions = new Actions(driver);

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

actions.moveToElement(moveonmenu);

actions.perform();

[/pastacode]

 

處理新視窗與切換

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

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

driver.switchTo().window(window2);

 

網頁元件是否可以運作

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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

System.out.print(fname);

[/pastacode]

透過 JavaScript改變網頁元件屬性

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]

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);

[/pastacode]

 

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. Required fields are marked *