每当在selenium中调用一个函数时,它会打开一个新窗口并且我的程序会卡住

时间:2016-02-18 05:16:47

标签: java selenium testing automated-tests

每当我在selenium中调用一个函数时,它会打开一个新窗口,我的程序会卡住, 一切都运行正常,但每当编译器进入函数时,就会打开新窗口,如果发生这种情况的话。

然后请建议任何解决方案,以便我可以在同一窗口中使用相同的功能,因为我无法从新打开的窗口开始再次给出地址。

public static void main(String[] args) throws InterruptedException {
    WebDriver driver=new FirefoxDriver();
    //Whenever compiler comes to this point, it displays an error
    function1(0,0,100);
    function1(0,1,100);
    function1(0,2,100);
    driver.findElement(By.xpath("xpath_address")).click();
    System.out.println("Test case executed successfully");
}
public static void function1(int x, int y, int z) throws InterruptedException{ 
    //Select field drop-down
    Thread.sleep(2000);
    WebDriver driver=new FirefoxDriver();
    Select oSelect=new Select(driver.findElement(By.xpath("xpath_address")));       
    Thread.sleep(1000);
    String S2=String.valueOf(x);
    oSelect.selectByValue(S2);
    //Select Operator drop-down
    Thread.sleep(2000);
    Select oSelect2=new Select(driver.findElement(By.xpath(" xpath_address")));     
    Thread.sleep(1000);
    String S3=String.valueOf(y);
    oSelect2.selectByValue(S3);
    // Provide input to value
    String S=String.valueOf(z);
    driver.findElement(By.xpath("xpath_address")).sendKeys(S);
    //Click on input Filters
    driver.findElement(By.xpath("xpath_address")).click();

}
}
}

3 个答案:

答案 0 :(得分:0)

问题出在WebDriver driver=new FirefoxDriver();。每次创建新的FirefoxDriver实例时,都会打开一个新窗口。将其传递给Main并将其作为参数发送到function1

public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    function1(driver, 0, 0, 100);
    function1(driver, 0, 1, 100);
    function1(driver, 0, 2, 100);
}

public static void function1(WebDriver driver, int x, int y, int z) throws InterruptedException {
    driver.findElement(By.xpath("xpath_address")).sendKeys(S);
} 

答案 1 :(得分:0)

  1. 删除函数中的WebDriver driver=new FirefoxDriver();
  2. private static WebDriver driver;声明在主要
  3. 之外
  4. driver=new FirefoxDriver(); - 只需在主

    中使用它
     private static WebDriver driver;
    
      public static void main(String[] args) throws InterruptedException {
        driver=new FirefoxDriver();
        //Whenever compiler comes to this point, it displays an error
        function1(0,0,100);
        function1(0,1,100);
        function1(0,2,100);
        driver.findElement(By.xpath("xpath_address")).click();
        System.out.println("Test case executed successfully");
     }
    
     public static void function1(int x, int y, int z) throws InterruptedException{ 
        //Select field drop-down
        Thread.sleep(2000);
        //WebDriver driver=new FirefoxDriver();
        Select oSelect=new Select(driver.findElement(By.xpath("xpath_address")));       
        Thread.sleep(1000);
        String S2=String.valueOf(x);
        oSelect.selectByValue(S2);
        //Select Operator drop-down
        Thread.sleep(2000);
        Select oSelect2=new Select(driver.findElement(By.xpath(" xpath_address")));     
        Thread.sleep(1000);
        String S3=String.valueOf(y);
        oSelect2.selectByValue(S3);
        // Provide input to value
        String S=String.valueOf(z);
        driver.findElement(By.xpath("xpath_address")).sendKeys(S);
        //Click on input Filters
        driver.findElement(By.xpath("xpath_address")).click();
    
                          }
    

答案 2 :(得分:0)

您必须稍微更改逻辑。首先在类中创建全局级别的静态webdriver。课后{

public static WebDriver driver;
主方法中的

启动驱动程序,如下所示

driver=new FirefoxDriver();

在该类的所有其他函数中,使用此驱动程序实例仅表示不创建另一个驱动程序对象。请删除函数中的以下声明

WebDriver driver=new FirefoxDriver();

通过这种方式,您只使用一个驱动程序实例,不会在每个新函数中打开新的浏览器

谢谢你, Murali G