在运行selenium脚本时,由于切换到其他区域,我的csv下载文件脚本被跳过

时间:2013-09-13 09:25:56

标签: selenium junit

正确运行下面的脚本,但是没有移动任何其他区域,但是如果执行任何操作,我的意思是移动到我的下载文件代码不起作用的任何其他区域。

问题与焦点有关还是需要进行一些更改。使用firefox 21版浏览器和selenium 2.33。请建议。

以下是代码:

package mypackage;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Before;
import org.junit.Test;
import Data.Function;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.After;

public class Report extends Function {

    Object Open_a_popup_window;
    String Positioned_Popup ;
    Object JavaScript_Popup_Windows ;

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "my url";
    }

    @Test
    public void Report1() throws Exception {
        Function object = new Function();
        object.Login();

        /* Enter Report Details */
        driver.get(baseUrl + "//");
        driver.get(baseUrl + "//");

        /*Run*/
        driver.findElement(By.xpath("")).click(); 
        Thread.sleep(30000);
        WebElement Action = driver.findElement(By.xpath(""));

        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
            ((JavascriptExecutor) driver).executeScript("window.focus();");}
            String linktxt = Action.getText();
            if (linktxt.equalsIgnoreCase("Record not found.")) {
                System.out.println("Report contains No Data:"); 
            }

            if (linktxt.equalsIgnoreCase("FAILED")) {
                System.out.println("Report failed");    
            } else {
                driver.findElement(By.xpath("/img")).click();

                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyPress(KeyEvent.VK_ENTER);
                Thread.sleep(10000);

                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyPress(KeyEvent.VK_ENTER);
                Thread.sleep(10000);

                System.out.println("Report Passed");
            }
        }
    }

    @After
    public void teardown() {
        driver.close();
        //System.exit(0);
    }
}

2 个答案:

答案 0 :(得分:0)

我看到你按下了Enter键使用了Robot,当然它会在有焦点的窗口上工作!

因此,一旦你搬出,就会在新聚焦的窗口上触发输入。

答案 1 :(得分:0)

弹出窗口是浏览器弹出窗口,它不是由机器人键处理的,因此我使用第三方应用程序“auto it”但它仅限于用于Windows操作系统的限制,unix或linux不支持。