Home > front end >  FAILED CONFIGURATION: @BeforeMethod setUp - Selenium Java TestNG
FAILED CONFIGURATION: @BeforeMethod setUp - Selenium Java TestNG

Time:05-31

I am trying to execute Selenium Java Project with TestNG but I am getting the Configurations Problems related to TestNG annotations. 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.

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.

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:

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

}

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

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

CodePudding user response:

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. Else it become null and throws null pointer exception.

@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 version and corresponding driver. ChromeDriver 102.0.5005.61

For me all the 3 test cases passes, except the first one where the authentication is not successful.

test results

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.

CodePudding user response:

Some addition answering "why LoginPage instance needs to be created in all test methods?". 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

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.

  • Related