我怎样才能成功通过此代码? Java Webdriver TestNg

How can I pass this code successfully? Java Webdriver TestNg

我想将此代码传递给我的其他 class,这样我就不必一直粘贴它了。

这是包含代码的class:

package utility;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserType {
    public static WebDriver driver;

    @Parameters("browser")
    @Test
    public static void CallBrowser(String browser) {

        if(browser.equalsIgnoreCase("firefox")) {

              driver = new FirefoxDriver();

          // If browser is IE, then do this   

          }else if (browser.equalsIgnoreCase("chrome")) { 

              // Here I am setting up the path for my IEDriver

              {System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
               driver = new ChromeDriver();

              driver.get(Constant.URL);

          } 

    }
}

我想将 CallBrowser 传递给下面的 SetUp

package automationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import utility.Constant;
import appModule.SignIn_Action;

public class SignIn {

    public WebDriver driver;



@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {

     if(browser.equalsIgnoreCase("firefox")) {

          driver = new FirefoxDriver();

      // If browser is Chrome, then do this   

      }else if (browser.equalsIgnoreCase("chrome")) { 

          { System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
          driver = new ChromeDriver();

          { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
          {driver.manage().window().maximize();}
           driver.get(Constant.URL);
      }
      } 

@Test
public  void signIn() {

    SignIn_Action.Execute(driver, Constant.DevStudentUsername, Constant.DevStudentPassword);      
 }  

@AfterMethod
public void Teardown() {
    driver.quit();

} 

  }

我试过只调用静态 class,但是 @BeforeMethod 抛出了一个配置错误,我确定我只是在传递驱动程序和参数时做错了。

有人可以解释一下我需要对两个 class 进行哪些更改才能使其正常工作吗?

这些是错误:

FAILED CONFIGURATION: @AfterMethod Teardown
java.lang.NullPointerException
    at automationFramework.SignIn.Teardown(SignIn.java:38)
    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:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

FAILED: signIn
java.lang.NullPointerException
    at appModule.SignIn_Action.Execute(SignIn_Action.java:27)
    at automationFramework.SignIn.signIn(SignIn.java:33)
    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:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
===============================================


===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

这就是我尝试调用代码的方式:

public WebDriver driver;



@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {

     BrowserType.CallBrowser(browser);
      } 

你的问题是你的 driver 在你调用 teardown 时没有初始化。您需要确保它在所有情况下都已初始化。我会用调试器检查你的代码并检查以确保当你点击设置和拆卸方法时 driver 实际上在安装时初始化并在拆卸时存在。当事情 运行 时,你也有两个 driver 存在。一个是您在代码的第一部分中声明的静态 driver,然后您有另一个 non-static driver 位于您的实际测试中。我会解决这个问题,因为它可能会导致问题。

解决问题的最简单方法是确保 driver 在安装过程中始终初始化,即使字符串与任一选项都不匹配。

public void SetUp(String browser) {

     if(browser.equalsIgnoreCase("firefox")) {

          driver = new FirefoxDriver();

      // If browser is Chrome, then do this   

      }else { 

          System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");
          driver = new ChromeDriver();

          { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
          {driver.manage().window().maximize();}
           driver.get(Constant.URL);
      }
}