@Factory注释未按需要执行testng类

时间:2018-08-21 14:51:27

标签: java selenium firefox selenium-webdriver testng

对于我当前的项目,我正在使用testng类和@factory来调用它们。以下是代码

test1

    public class Test1 extends Setup{
      @Test
      public void facebook() {
          System.out.println("Inside Facebook");
      }

      @BeforeClass
      public void beforeClass() {
          driver.get("https://www.facebook.com/");
      }

      @AfterClass
      public void afterClass() {
            driver.close();
            driver.quit();
      }
}  

test2

public class Test2 extends Setup{
  @Test
  public void google() {
      System.out.println("Inside Google");
  }

  @BeforeClass
  public void beforeClass() {
      driver.get("https://www.google.com/");
  }

  @AfterClass
  public void afterClass() {
      driver.close();
      driver.quit();
  }

setup

public class Setup {

    public static WebDriver driver;

    public Setup(){
        initialise();
    }

    public void initialise()
    {
//      SELECT THE CUSTOM PROFILE FOR SKIPPING THE TRUST CERTIFICATE
        ProfilesIni prof = new ProfilesIni();               
        FirefoxProfile ffProfile= prof.getProfile("MyProfile");

//      ADDING THE PAGE ON EXCETION
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);

//      INVOKE THE DRIVER
        driver = new FirefoxDriver(ffProfile);
    }  

Factory Class

public class Driver {
    @Factory
    public Object[] objInitialise(){
        ArrayList<Object> module = new ArrayList<Object>();
        module.add(new Test1());
        module.add(new Test2());
        return module.toArray();
    }  

现在,在运行driver类之后,它首先打开一个具有URL:about:blank&utm_content=firstrun的浏览器,然后打开另一个具有所需URL:https://www.facebook.com/的浏览器。

在eclipse控制台中,我看到以下错误

org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'

Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:377)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:644)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:316)
    at TestMavenGroupId.TestMavenArtifactId.Test2.beforeClass(Test2.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:166)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

以下应该是我正在寻找的实际输出

Inside Facebook
Inside Google  

能帮我了解我在想什么。我没有得到正确的输出。我希望为每个测试打开单独的浏览器,并在各自的浏览器上依次启动该URL。

硒版本:2.53.1
Firefox版本:47.0.1

1 个答案:

答案 0 :(得分:0)

弄清楚了,我不得不在各自的测试类中初始化驱动程序,并避免使用driver.quit(),这是因为driver被声明为static。现在,下面是测试代码

    public class Test1 extends Setup{
      @Test
      public void facebook() {
          System.out.println("Inside Facebook");
      }

      @BeforeClass
      public void beforeClass() {
          driver = new FirefoxDriver();
          driver.get("https://www.facebook.com/");
      }

      @AfterClass
      public void afterClass() {
            driver.close();
      }
   }