Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SELENIUM无法关闭新选项卡_Java_Selenium - Fatal编程技术网

Java SELENIUM无法关闭新选项卡

Java SELENIUM无法关闭新选项卡,java,selenium,Java,Selenium,我一直在开发selenium驱动程序,我必须关闭新选项卡,否则当前的测试用例将由于无法分配xpath目录而失败。我注意到我打了3次电话给webdriver,有人能帮我解决我犯的错误吗?请告知。先谢谢你 签署行动: public class SignIn_ActionBuilder { static WebDriver wd = new FirefoxDriver(); public static void Execute(WebDriver driver) throws Exc

我一直在开发selenium驱动程序,我必须关闭新选项卡,否则当前的测试用例将由于无法分配xpath目录而失败。我注意到我打了3次电话给webdriver,有人能帮我解决我犯的错误吗?请告知。先谢谢你

签署行动:

public class SignIn_ActionBuilder {
    static WebDriver wd = new FirefoxDriver();

    public static void Execute(WebDriver driver) throws Exception{

        wd.get(Constant.URL);

        wd.manage().window().maximize();

        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        Home_Page.Skip_Advertising(wd).click();

        Home_Page.lnk_MyAccount(wd).click();

        LogIn_Page.txtbx_UserName(wd).sendKeys(Constant.Username);

        LogIn_Page.txtbx_Password(wd).sendKeys(Constant.Password);

        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        LogIn_Page.btn_LogIn(wd).click();

        wd.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);




    } 
}
产品选择:

public class ProductSelectionConfirmation_Action {
    static WebDriver wd = new FirefoxDriver();



     public static void ThreeDigit_Execute(WebDriver driver) throws Exception{

            // This is to get the Product name on the Confirmation page with using getText()/click method 
            // Once some text is stored in this variable can be used later in any other class 

            wd.manage().wait(120);
            wd.close();

            ConfirmationPlaceBet_Page.pick_PickLotteryNum1(wd).click();
            ConfirmationPlaceBet_Page.pick_PickLotteryNum2(wd).click();
            ConfirmationPlaceBet_Page.pick_PickLotteryNum3(wd).click();

            ConfirmationPlaceBet_Page.btn_ConfirmNumberToBet(wd).click();

             for (int i = 0; i < 49; i++) {
                ConfirmationPlaceBet_Page.btn_IncreaseBet(wd).click();
            } 

            ConfirmationPlaceBet_Page.btn_ProceedBet(wd).click();

            ConfirmationPlaceBet_Page.btn_ConfirmBet(wd).click();
            // This is all about Verification checks, these does not stop your execution but simply report fail at the end
            // This is to check that if the value in the variable pick_PickLotteryNum1 is not null, then do this

         }
}

我可以看到您发布的代码存在一系列问题。 在每个动作类中,您都在创建一个新的静态web驱动程序对象

static WebDriver wd = new FirefoxDriver();
这意味着调用该类时,它将打开一个新的Firefox浏览器。 您还将从测试用例向execute方法传递一个webdriver对象。但是传递的webdriver从未在execute方法中使用

 public static void ThreeDigit_Execute(WebDriver driver) throws Exception{}
public class Sobet_WBG_YiWanCai {
public WebDriver driver;

@Test(description = "WBG亿万彩 - 后三码" , enabled = true)
  public void f() throws Exception {
      try{
          //Create the driver instance here.
          driver = new FirefoxDriver();
          SignIn_ActionBuilder.Execute(driver);
          ProductSelectionConfirmation_Action.ThreeDigit_Execute(driver);
          Home_Page.lnk_LogOut(driver);
          Home_Page.btn_LogOutDialog(driver);
          driver.close();
      }catch (Exception e){

          Log.error(e.getMessage());
          throw (e);
      }

  }
}
方法中的任何操作都不使用
driver
对象,而是在整个方法中使用
wd
对象

更正了第一类执行方法的代码:

 public class SignIn_ActionBuilder {

    public static void Execute(WebDriver driver) throws Exception{

       driver.get(Constant.URL);

       driver.manage().window().maximize();

       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

       Home_Page.Skip_Advertising(driver).click();

       Home_Page.lnk_MyAccount(driver).click();

       LogIn_Page.txtbx_UserName(driver).sendKeys(Constant.Username);

       LogIn_Page.txtbx_Password(driver).sendKeys(Constant.Password);

       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

       LogIn_Page.btn_LogIn(driver).click();

       driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
   }
} 
从测试用例中,您必须创建一个webdriver对象,并将其传递给execute方法

 public static void ThreeDigit_Execute(WebDriver driver) throws Exception{}
public class Sobet_WBG_YiWanCai {
public WebDriver driver;

@Test(description = "WBG亿万彩 - 后三码" , enabled = true)
  public void f() throws Exception {
      try{
          //Create the driver instance here.
          driver = new FirefoxDriver();
          SignIn_ActionBuilder.Execute(driver);
          ProductSelectionConfirmation_Action.ThreeDigit_Execute(driver);
          Home_Page.lnk_LogOut(driver);
          Home_Page.btn_LogOutDialog(driver);
          driver.close();
      }catch (Exception e){

          Log.error(e.getMessage());
          throw (e);
      }

  }
}

您必须删除
静态WebDriver wd=newfirefoxdriver()所有动作类的行。

您的意思是
driver.close()不工作?您遇到了什么错误?@HelpingHands我没有收到任何错误,它不会关闭新选项卡无需调用web驱动程序3次..只需将
public static WebDriver driver=null在“Sobet_WBG_YiWanCai”类中,好的,首先按照上面的方法操作,然后看看它是否有效。@HelpingHands我在pageObjects.ConfirmationPlaceBet_Page.pick_PickLotteryNum1(ConfirmationPlaceBet_Page.java:70)上得到java.lang.NullPointerException的错误。我确实删除了静态WebDriver wd=new FirefoxDriver();但由于获取driver.get(Constant.URL)的空值,因此失败;