配置失败:@BeforeMethod setUp - Selenium Java TestNG

FAILED CONFIGURATION: @BeforeMethod setUp - Selenium Java TestNG

我正在尝试使用 TestNG 执行 Selenium Java 项目,但我遇到了与 TestNG 注释相关的配置问题。我正在使用页面对象模型设计模式。 chromedriver 为每个测试用例成功执行并登陆登录页面,但之后它崩溃并给出有关配置失败@BeforeMethod 的错误。

我正在分享整个代码和控制台错误。对于这个人的任何解决方案!卡在这里好久了

第类页.

基页:

package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public class BasePage{

public WebDriver driver;
public WebDriverWait wait;  

public BasePage(WebDriver driver, WebDriverWait wait) {
    
    this.driver = driver;
    this.wait = wait;
}

//Generic Methods

public void click(By elementlocation) {
    driver.findElement(elementlocation).click();
}

public void writeText(By elementlocation, String text) {
    driver.findElement(elementlocation).sendKeys(text);
}

public String readText(By elementlocation) {
    return driver.findElement(elementlocation).getText();
}

public void waitVisibility(By elementlocation) {
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(elementlocation));
}

public void waitElementToBeClickable(By elementlocation ) {
    wait.until(ExpectedConditions.elementToBeClickable(elementlocation));
}

public void assertEquals (By elementlocation, String expectedText) {
    waitVisibility(elementlocation);
    Assert.assertEquals(readText(elementlocation), expectedText);
}
    
}

登录页面方法:

package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage extends BasePage{
    
    public LoginPage(WebDriver driver, WebDriverWait wait) {
        super(driver, wait);
    }

    By email = By.xpath("//input[@data-placeholder='Enter your username']");
    By pwd = By.xpath("//input[@data-placeholder='Enter your Password']");
    By LoginBtn = By.xpath("//button//span[text()='Login']");
    By emptyLoginVerification = By.xpath("//span[contains(text(),'Both Password and user name are required.')]");
    By emptyPasswordVerification = By.xpath("//span[contains(text(),'Both Password and user name are required.')]");
    By emptyUserNameVerification = By.xpath("//span[contains(text(),'Both Password and user name are required.')]");

    //methods
    public LoginPage LoginToAMS(String username, String password) {
        
        writeText(email, username);
        
        writeText(pwd, password);
        
        click(LoginBtn);
                
        return this;
    }
    
    public LoginPage EmptyLoginVerification (String expectedText) {
        
        assertEquals(emptyLoginVerification, expectedText);
        
        return this;    
    }
    
    public LoginPage EmptyPasswordVerification (String expectedText) {
        
        assertEquals(emptyPasswordVerification, expectedText);
        
        return this;        
    }
    
    public LoginPage EmptyUserNameVerification (String expectedText) {
        
        assertEquals(emptyUserNameVerification, expectedText);
        
        return this;        
    }
}

下面是测试类现在。

基础测试:

package tests;

import java.time.Duration;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;


public class BaseTest {
    
    public WebDriver driver;
    public WebDriverWait wait;
    

    @BeforeMethod
    public void setup() {
        
        System.setProperty("WebDriver.chrome.driver", "C:\eclipse\eclipse-workspace\ams\chromedriver.exe");
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(15));
        driver.manage().window().maximize();
        driver.get("http://dummyweb.com");
        
    }
    
    @AfterMethod
    public void teardown() {
        driver.close();
    }
}

这里是实际的 TestClass:

package tests;

import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import Pages.HomePage_ListOfItems;
import Pages.LoginPage;

public class LoginTestCases extends BaseTest{
    
    LoginPage loginpage = new LoginPage(driver, wait);
    
    private String username = "nasir.n@planet.com";
    private String password = "123456";
    
    @Test(priority=0)
    public void Valid_Login_To_AMS() {
        loginpage.LoginToAMS(username, password);   
    }
    
    @Test(priority=1)
    public void Empty_Login_To_AMS() {
        loginpage.LoginToAMS("","");
        loginpage.EmptyLoginVerification("Both Password and user name are required.");
    }
    
    @Test(priority=2)
    public void UserName_EmptyPassword() {
        loginpage.LoginToAMS(username,"");
        loginpage.EmptyPasswordVerification("Both Password and user name are required.");
    }
    
    @Test(priority=4)
    public void EmptyUserName_PasswordFill( ) {
        loginpage.LoginToAMS("", password);
        loginpage.EmptyUserNameVerification("Both Password and user name are required.");
    }

}

我得到的错误如下:

[RemoteTestNG] detected TestNG version 7.4.0
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 52405
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
May 30, 2022 8:45:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
May 30, 2022 8:45:44 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 101, so returning the closest version found: a no-op implementation
May 30, 2022 8:45:44 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Unable to find CDP implementation matching 101.
May 30, 2022 8:45:44 PM org.openqa.selenium.chromium.ChromiumDriver lambda$new
WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.1.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
Starting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 50748
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
May 30, 2022 8:46:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
May 30, 2022 8:46:31 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 101, so returning the closest version found: a no-op implementation
May 30, 2022 8:46:31 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Unable to find CDP implementation matching 101.
May 30, 2022 8:46:31 PM org.openqa.selenium.chromium.ChromiumDriver lambda$new
WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.1.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
FAILED CONFIGURATION: @BeforeMethod setup
org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
from disconnected: unable to send message to renderer
  (Session info: chrome=101.0.4951.67)
Build info: version: '4.1.1', revision: 'e8fcc2cecf'
System info: host: 'NASIR-S-LTP', ip: '192.168.10.18', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [afb96e2642d9f544c41d7d722caab5d3, get {url=http://dev-ims.dplit.com/}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 101.0.4951.67, chrome: {chromedriverVersion: 101.0.4951.41 (93c720db8323..., userDataDir: C:\Users\nasir.n\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:53544}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:53544/devtoo..., se:cdpVersion: 101.0.4951.67, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: afb96e2642d9f544c41d7d722caab5d3
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:312)
    at tests.BaseTest.setup(BaseTest.java:29)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:700)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>amsdev</groupId>
  <artifactId>ams</artifactId>
  <version>0.0.1-SNAPSHOT</version>

 <dependencies>


    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>



  </dependencies>

</project>

当我复制粘贴您的代码时,我发现系统 属性 设置错误。正确的应该是

System.setProperty("webdriver.chrome.driver", "C:\UserTemp\tools\chrome\chromedriver.exe");

注意这里的小案例。

我做的第二件事是在每个测试方法中初始化登录页面。否则它变为 null 并抛出空指针异常。

@Test(priority = 4)
    public void EmptyUserName_PasswordFill() {
        LoginPage loginpage = new LoginPage(driver, wait);
        loginpage.LoginToAMS("", password);
        loginpage.EmptyUserNameVerification("Both Password and user name are required.");
    }

我没有 chrome 所以下载了最新版本 和相应的驱动程序。 ChromeDriver 102.0.5005.61

对我来说,除了第一个验证不成功之外,所有 3 个测试用例都通过了。

请注意,我有 edge 浏览器和正确的驱动程序,测试也在 edge 中通过。

所有测试方法中要创建的loginPage实例就是我为了解决空指针异常所做的改动

一些补充回答“为什么需要在所有测试方法中创建 LoginPage 实例?”。您应该在驱动程序初始化后初始化您的页面对象。

在您的示例中,TestNg 创建了一个测试实例 class,其中字段

LoginPage loginpage = new LoginPage(driver, wait);

立即初始化。那时您的设置方法尚未执行,因此您将 null 作为 driver 传递给您的页面对象。