如何使用WebDriver处理OS生成的弹出窗口

时间:2013-03-26 08:31:06

标签: java webdriver autoit

当我输入错误的用户名和密码时,一个弹出窗口正在发出消息,一个Ok按钮就在那里阻止了我的应用程序。但我无法找到该按钮的ID,因为firebug没有在那个工作。我搜索了更多的博客,我开始知道它是由OS生成的弹出窗口,它可以由AutoIT处理。请帮助我如何处理弹出框。这是我的代码。

![package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Read {

    public WebDriver driver;

    @BeforeMethod
    public void launch() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    public void testImportexport1() throws BiffException, IOException,
            RowsExceededException, WriteException, InterruptedException {

        FileInputStream fis = new FileInputStream("Data//Logindev.xls");
        Workbook w = Workbook.getWorkbook(fis);
        Sheet s = w.getSheet(0);
        String a\[\]\[\] = new String\[s.getRows()\]\[s.getColumns()\];

        FileOutputStream fos = new FileOutputStream("Data//Logindev_1.xls");
        WritableWorkbook wwb = Workbook.createWorkbook(fos);
        WritableSheet ws = wwb.createSheet("LoginResult", 0);

        System.out.println("s.getRows() = " + s.getRows());

        for (int i = 0; i < s.getRows(); i++) {
            System.out.println("s.getColumns() = " + s.getColumns());

            for (int j = 0; j < s.getColumns(); j++) {
                a\[i\]\[j\] = s.getCell(j, i).getContents();
                Label l = new Label(j, i, a\[i\]\[j\]);
                Label l1 = new Label(2, 0, "Result");

                ws.addCell(l);
                ws.addCell(l1);


            }
        }
        for (int i = 1; i < s.getRows(); i++) {
            driver.get("url");

            driver.findElement(By.name("txtUserName")).sendKeys(
                    s.getCell(0, i).getContents());
            driver.findElement(By.name("txtPwd")).sendKeys(
                    s.getCell(1, i).getContents());
            driver.findElement(By.name("btnSignIn")).click();

            Thread.sleep(15000);

            // System.out.println("Title = " + driver.getTitle());
            if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) {
                System.out.println("Element is found");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.findElement(
                        By.xpath("//*\[@id='ctl00_headerContent_lnkLogOut'\]"))
                        .click();
                Thread.sleep(2000);
                Label l2 = new Label(2, i, "Pass");
                ws.addCell(l2);
            } else if (driver.getTitle().equalsIgnoreCase(
                    "Welcome to ShopMyFarm")) {

                driver.switchTo().alert().accept();
                Thread.sleep(5000);
                System.out.println("Element Not Found");
                Label l2 = new Label(2, i, "Fail");
                ws.addCell(l2);
            }
        }
        Thread.sleep(2000);
        wwb.write();
        wwb.close();
    }
}][1]

1 个答案:

答案 0 :(得分:0)

WebDriver无法直接与对话框窗口交互,这是因为对话框窗口是操作系统的域而不是网页。我认为您可以在java中使用Robot类来执行您想要执行的键盘操作,例如按Enter键关闭操作系统对话框。

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

public class RobotExp 
{ 

  public static void main(String[] args) 
  { 

    try { 

          Robot robot = new Robot(); 
          robot.keyPress(KeyEvent.VK_ENTER); 
         } 
    catch (AWTException e) { 
           e.printStackTrace(); 
         } 
    } 
 }