您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium處理模態(tài)對(duì)話框
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/5/20 9:48:46 ] 推薦標(biāo)簽:功能測(cè)試

  問(wèn)題描述:
  點(diǎn)擊按鈕出現(xiàn)一個(gè)模態(tài)對(duì)話框,代碼會(huì)卡在click這步不繼續(xù)執(zhí)行。原因是Selenium目前沒(méi)有提供對(duì)模態(tài)對(duì)話框的處理。
  解決方案:
  將click出現(xiàn)彈出框這步用JS代替執(zhí)行,然后切換到彈出窗可以繼續(xù)操作頁(yè)面元素了。
  測(cè)試地址: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();    //點(diǎn)擊后代碼卡在這里
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();    //點(diǎn)擊后代碼卡在這里
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();
}
}

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