您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium處理模態(tài)對話框
作者:網絡轉載 發(fā)布時間:[ 2015/5/20 9:48:46 ] 推薦標簽:功能測試

  問題描述:
  點擊按鈕出現一個模態(tài)對話框,代碼會卡在click這步不繼續(xù)執(zhí)行。原因是Selenium目前沒有提供對模態(tài)對話框的處理。
  解決方案:
  將click出現彈出框這步用JS代替執(zhí)行,然后切換到彈出窗可以繼續(xù)操作頁面元素了。
  測試地址:https://developer.mozilla.org/samples/domref/showModalDialog.html
  代碼如下:
public class junitTest {
WebDriver driver = new FirefoxDriver();
String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";
@Test
public void openModal() throws InterruptedException{
driver.get(baseUrl);
driver.findElement(By.xpath("/html/body/input")).click();    //點擊后代碼卡在這里
Thread.sleep(2000);
Set<String> handlers = driver.getWindowHandles();
for(String winHandler:handlers){
driver.switchTo().window(winHandler);
}
driver.findElement(By.id("foo")).sendKeys("2");
driver.findElement(By.xpath("/html/body/input[2]")).click();
}
}
  click這步替換為JS執(zhí)行后代碼:
public class junitTest {
WebDriver driver = new FirefoxDriver();
String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";
@Test
public void openModal() throws InterruptedException{
driver.get(baseUrl);
//driver.findElement(By.xpath("/html/body/input")).click();    //點擊后代碼卡在這里
String js = "setTimeout(function(){document.getElementsByTagName('input')[0].click()},100)";
((JavascriptExecutor)driver).executeScript(js);
Thread.sleep(2000);
Set<String> handlers = driver.getWindowHandles();
for(String winHandler:handlers){
driver.switchTo().window(winHandler);
}
driver.findElement(By.id("foo")).sendKeys("2");
driver.findElement(By.xpath("/html/body/input[2]")).click();
}
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd