SELENIUM无法关闭新标签

时间:2015-10-19 06:47:52

标签: java selenium

我一直在研究selenium驱动程序,我必须关闭新选项卡,否则当前的Testcase将因为无法分配xpath目录而失败。我注意到我打了三次网络驱动程序,任何人都可以引导我完成我犯的错误吗?好心提醒 。提前谢谢你

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




    } 
}

产品选择:

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

      }
}

1 个答案:

答案 0 :(得分:1)

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

static WebDriver wd = new FirefoxDriver();

这意味着它会在调用类时打开一个新的Firefox浏览器。 此外,您还将webdriver对象传递给测试用例的execute方法。但传递的webdriver永远不会在execute方法中使用。

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

您没有将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 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);
      }

  }
}

您必须从所有操作类中删除static WebDriver wd = new FirefoxDriver();行。