我正在嘗試使用 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;
}
}
下面是現在的TestClasses。
基礎測驗:
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 = "[email protected]";
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$3
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$3
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>
uj5u.com熱心網友回復:
當我復制粘貼您的代碼時,我看到系統屬性設定錯誤。正確的應該是
System.setProperty("webdriver.chrome.driver", "C:\\UserTemp\\tools\\chrome\\chromedriver.exe");
注意這里的小案例。
我做的第二件事是在每個測驗方法中初始化 LoginPage。否則它變為 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 個測驗用例都通過了,除了第一個身份驗證不成功的測驗用例。

請注意,我有邊緣瀏覽器和正確的驅動程式,測驗也通過了邊緣。
在所有測驗方法中要創建的 loginPage 實體是我為解決空指標例外所做的更改。
uj5u.com熱心網友回復:
一些補充回答“為什么需要在所有測驗方法中創建 LoginPage 實體?”。您應該在初始化驅動程式后初始化頁面物件。
在您的示例中,TestNg 創建了您的測驗類的實體,其中欄位
LoginPage loginpage = new LoginPage(driver, wait);
立即初始化。到那時,您的設定方法尚未執行,因此您將null其傳遞driver給您的頁面物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/488606.html
