简体   繁体   English

硒无法关闭新标签

[英]SELENIUM Unable to close new tab

i been working on the selenium driver which i have to close the new tab else the current Testcase will fail due unable to allocate the directory of xpath.我一直在研究 selenium 驱动程序,我必须关闭新选项卡,否则当前的测试用例将因无法分配 xpath 目录而失败。 I notice that im calling 3 times of the webdriver , can anyone guide me through the mistake i made?我注意到我调用了 3 次 webdriver,有人可以指导我解决我犯的错误吗? Kindly advise .好心提醒 。 Thanks you in advance提前谢谢你

SignIn_Action:登录_操作:

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);




    } 
}

Product Selection :产品选择:

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

         }
}

TestCase :测试用例 :

public class Sobet_WBG_YiWanCai {
    public WebDriver driver;

    @Test(description = "WBG亿万彩 - 后三码" , enabled = true)
      public void f() throws Exception {
          try{
              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);
          }

      }
}

I can see a series of issues with the code you have posted.我可以看到您发布的代码存在一系列问题。 In each of the Action classes you are creating a new static web driver object.在每个 Action 类中,您都在创建一个新的静态 Web 驱动程序对象。

static WebDriver wd = new FirefoxDriver();

Which means it will open a new Firefox browser when the class is called.这意味着它会在调用该类时打开一个新的 Firefox 浏览器。 And also you are passing a webdriver object in to the execute methods from the test case.而且您还将一个 webdriver 对象传递给测试用例中的执行方法。 But the passed webdriver is never used in the execute methods.但是传递的 webdriver 永远不会在执行方法中使用。

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

You are not using the driver object for any action in the method but uses the wd object throughout the method.您没有将driver对象用于方法中的任何操作,而是在整个方法中使用wd对象。

Corrected code for the first class execute method :更正了第一类执行方法的代码:

 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);
   }
} 

And from the test case you have to create a webdriver object and pass it into the execute methods.从测试用例中,您必须创建一个 webdriver 对象并将其传递给执行方法。

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);
      }

  }
}

And you have to remove the static WebDriver wd = new FirefoxDriver();并且您必须删除static WebDriver wd = new FirefoxDriver(); line from all your action classes.来自您所有操作类的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM