简体   繁体   English

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

[英]FAILED CONFIGURATION: @BeforeMethod setUp - Selenium Java TestNG

I am trying to execute Selenium Java Project with TestNG but I am getting the Configurations Problems related to TestNG annotations.我正在尝试使用 TestNG 执行 Selenium Java 项目,但我遇到了与 TestNG 注释相关的配置问题。 I am using Page object model design pattern.我正在使用页面对象模型设计模式。 The chromedriver gets executed successfully for each testcase and it lands on login page but after that it crashes and gives error about Failed Configurations @BeforeMethod. chromedriver 为每个测试用例成功执行并登陆登录页面,但之后它崩溃并给出有关失败配置@BeforeMethod 的错误。

I am sharing the whole code and console errors.我正在分享整个代码和控制台错误。 Any Solution for this folks!这个人的任何解决方案! Been stuck here for the long time.卡在这里很久了。

Page Classes.页面类。

Base Page:基页:

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

Login Page with Methods:带有方法的登录页面:

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

Below are the TestClasses Now.下面是现在的TestClasses。

BaseTest:基础测试:

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

And here is the actual TestClass:这是实际的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.");
    }

}

The errors that I am getting are below:我得到的错误如下:

[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 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>

When I copy paste your code, I see the system property is set wrong.当我复制粘贴您的代码时,我看到系统属性设置错误。 The correct one should be正确的应该是

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

Notice the small cases here.注意这里的小案例。

Second thing I did was to Initialize the LoginPage In every test method.我做的第二件事是在每个测试方法中初始化 LoginPage。 Else it become null and throws null pointer exception.否则它变为 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.");
    }

I did not had chrome so downloaded the latest version我没有chrome所以下载了最新版本镀铬版 and corresponding driver.和相应的驱动程序。 ChromeDriver 102.0.5005.61 ChromeDriver 102.0.5005.61

For me all the 3 test cases passes, except the first one where the authentication is not successful.对我来说,所有 3 个测试用例都通过了,除了第一个身份验证不成功的测试用例。

试验结果

Please note that I had edge browser and with correct driver, the test passes in edge as well.请注意,我有边缘浏览器和正确的驱动程序,测试也通过了边缘。

The loginPage instance to be created in all the test methods is the change I did to solve the null pointer exception.在所有测试方法中要创建的 loginPage 实例是我为解决空指针异常所做的更改。

Some addition answering "why LoginPage instance needs to be created in all test methods?".一些补充回答“为什么需要在所有测试方法中创建 LoginPage 实例?”。 You should initialize your page object after your driver has been initialized.您应该在初始化驱动程序后初始化页面对象。

In your example TestNg creates an instance of your test class where the field在您的示例中,TestNg 创建了您的测试类的实例,其中字段

LoginPage loginpage = new LoginPage(driver, wait);

is initialized right away.立即初始化。 By that moment your set up method has not yet been executed hence you pass null as driver to your page object.到那时,您的设置方法尚未执行,因此您将null作为driver传递给您的页面对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM