当前位置: 首页 > 知识库问答 >
问题:

我怎样才能成功地通过这个代码?Java Webdriver测试

陆高峰
2023-03-14

我想把这段代码传递给我的其他类,这样我就不必继续粘贴它了。

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

          } 

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

} 

  }

以下是错误:

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

共有1个答案

彭仲卿
2023-03-14

您的问题是,当您调用TearDown时,您的驱动程序没有初始化。您需要确保在所有情况下都初始化了它。我将使用调试器检查您的代码,并进行检查,以确保当您单击setup and teardown方法时,驱动程序实际上是在setup时初始化的,并且在deordown时存在的。当事情运行时,你也有两个司机在场。一个是在代码的第一部分声明的静态驱动程序,然后在实际测试中找到另一个非静态驱动程序。我会解决这个问题,因为它可能会引起问题。

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

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);
      }
} 
 类似资料: