Driver giving Null point exception - java

The below code was trying to run without example keyword in cucumber, but the output is dispalying as a Null pointer error
WebDriver driver;
#Given("^user is alredy in login page$")
public void user_is_alredy_in_login_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\swagatika.mohapatra\\OneDrive - Qualitest Group\\Desktop\\selenium\\DRIVER\\D-v-88-chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
}
#Given("^user enters \"(.*)\" and \"(.*)\"$")
public void user_enters_valid_user_name(String username, String password) {
this.driver = driver;
driver.findElement(By.id("txtUsername")).sendKeys(username);
driver.findElement(By.id("txtPassword")).sendKeys(password);
}
console output in Debug mode -
this = {LoginStepDefination#3223}
driver = null
username = "Admin"
password = "admin123"
this.driver = null
driver = null

WebDriver driver = new ChromeDriver();
you are redeclaring driver as a local varaible in first step instead:
WebDriver driver;
#Given("^user is alredy in login page$")
public void user_is_alredy_in_login_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\swagatika.mohapatra\\OneDrive - Qualitest Group\\Desktop\\selenium\\DRIVER\\D-v-88-chrome\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
}
#Given("^user enters \"(.*)\" and \"(.*)\"$")
public void user_enters_valid_user_name(String username, String password) {
this.driver.findElement(By.id("txtUsername")).sendKeys(username);
this.driver.findElement(By.id("txtPassword")).sendKeys(password);
}

Related

java.lang.NullPointerException when using webdriver manager selenium java

While using selenium with java, WebdriverManager is not running and the below code is giving null pointer exception. I have returned the driver at end of class.
I have one ask whether should I keep the Webdriver driver as static or not.
import io.github.bonigarcia.wdm.WebDriverManager;
public class Browserselector {
public WebDriver driver;
public static Properties prop;
public WebDriver initializeDriver() throws IOException {
{
String browserName = "firefox";
System.out.println(browserName);
if (browserName.contains("Chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.contains("IE")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if (browserName.contains("FireFox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browserName.contains("EDGE")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("google.com");
return driver;
}
}
Thanks for your help in advance.
you are trying to start "firefox" - but the if condition checks for "Firefox", if you want to use it like that change the following condition
browserName.contains("FireFox")
into
browserName.equalsIgnoreCase("FireFox")
I recommend you to change the nested if with a "switch" it's more readable and easy to follow/understand
Also, don't use a URL without specifying the protocol
driver.get("https://www.google.com");

TestNG Selenium: Why does #Before work fine but #BeforeTest throws a NullPointerException?

This is my step definition class which is failing, the first #Test where the driver gets the URL is throwing a NullPointerException:
public class stepDefinitionASOS extends base {
AsosElements ae;
#BeforeTest
public void initializeTest() throws IOException {
driver = initializeDriver();
PageFactory.initElements(driver, this);
driver.manage().window().maximize();
ae = new AsosElements(driver);
}
#Test
#Given("^User goes to the \"([^\"]*)\" website$")
public void user_navigates_to_the_ASOS_website(String url) throws Throwable {
driver.get(url);
}
#Test
#Given("^User navigates to the account page$")
public void user_navigates_to_the_account_page() throws Throwable {
ae.clickAsosMyAccountDropdown(driver);
ae.clickAsosMyAccountButton(driver);
}
#Test
#When("^User logs in with (.+) and (.+)$")
public void user_logs_in_with_username_and_password(String username, String password) throws Throwable {
ae.typeEmailAddress(username);
ae.typePassword(password);
ae.clickSignInButton();
}
#Test
#Then("^Login should be successful$")
public void login_successful_is_something() throws Throwable {
Assert.assertTrue(ae.getMyAccountTitle().isDisplayed());
}
#AfterTest
public void teardown() {
driver.close();
driver = null;
}
}
The tests seem to run fine when I use the #Before Cucumber annotation instead of #BeforeTest TestNG annotation.
This is the base class that the step definition class is inheriting from:
public class base {
public WebDriver driver;
public Properties prop;
public static final String USERNAME = ""; // I have blanked this out for security reasons
public static final String AUTOMATE_KEY = ""; // I have blanked this out for security reasons
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "#hub-cloud.browserstack.com/wd/hub";
public WebDriver initializeDriver() throws IOException {
DesiredCapabilities caps = new DesiredCapabilities();
DesiredCapabilities capability = new DesiredCapabilities();
caps.setCapability("os", "OS X");
caps.setCapability("os_version", "High Sierra");
caps.setCapability("browser", "Firefox");
caps.setCapability("browser_version", "54.0");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
driver = new RemoteWebDriver(new URL(URL), caps);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
return driver;
}
Feature file:
#AsosLogin
Feature: Logging into ASOS
Scenario Outline: Going to ASOS website and logging in correct details
Given User goes to the "https://www.asos.com" website
And User navigates to the account page
When User logs in with <username> and <password>
Then Login should be successful
Examples:
|username |password |
|random#hotmail.co.uk |password |
StackTrace:
java.lang.NullPointerException at
stepDefinitions.stepDefinitionASOS.user_navigates_to_the_ASOS_website(stepDefinitionASOS.java:24)
at ✽.Given User goes to the "https://www.asos.com"
website(C:/Users/aliba/Documents/googleTingYaKnaDisOne/src/test/java/features/asosLogin.feature:5)
Does anyone know how I can go about fixing this?

Run MARIONETTE instead gecko

I'm trying to run selenium with marionette, but after run nothing showing
#Before
public void setup() {
FirefoxOptions options= new FirefoxOptions();
options.setCapability(FirefoxDriver.MARIONETTE, false);
driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("www.google.com");
}
#Test
public void testLoginAndHomePage() {
login = new LoginPage(driver);
String loginPageTitle = login.getLoginTitle();
Assert.assertTrue(loginPageTitle.contains("GOOGLE"));
System.out.println("Test Passed");
}
Is it possible to run marionette without System.setProperty() ?

NullPointerException when running my test with page factory

I get an exception when I run my test. I am using selenium with page factory. When I run following code ,it will open up the website and fail with exception below. it doesn't perform the HomePage.ClickbtnCookieWarning() in my test case.
Can someone please help me to understand why my code isn't working?
FAILED CONFIGURATION: #BeforeTest SetUp java.lang.NullPointerException
at
org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at
org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy5.click(Unknown Source) at
pageObjects.HomePage.ClickLoginLink(HomePage.java:57) at
myaccountsuite.TC1DefaultDeliveryAddDisplay.SetUp(TC1DefaultDeliveryAddDisplay.java:29)
Home Page page object
public class HomePage {
WebDriver driver;
public HomePage (WebDriver driver)
{
this.driver=driver;
}
#FindBy(id="ctl00_header_hdrCookieWarning_btnHideCookieWarning")
WebElement btnCookieWarning;
#FindBy(xpath=".//*#id='ctl00_masterContainerTop_Block_637_LoginView1_ulAnonymous']/li[2]/a")
WebElement LoginLink;
public void ClickbtnCookieWarning()
{
btnCookieWarning.click();
}
public void ClickLoginLink()
{
LoginLink.click();
}
}
Login Page Object
public class login {
WebDriver driver;
public login(WebDriver driver)
{
this.driver = driver;
}
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_UserName")
WebElement UserName;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_Password")
WebElement Password;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_LoginButton")
WebElement btn_LogIn;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_txtAccount")
WebElement Account;
#FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_btnHomeBranch_3")
WebElement btn_Continue;
public void userLogin(String uname, String pass, String acc)
{
UserName.sendKeys(uname);
Password.sendKeys(pass);
btn_LogIn.click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Account.sendKeys(acc);
btn_LogIn.click();
btn_Continue.click();
}
}
My Test
public class TC1DefaultDeliveryAddDisplay {
public WebDriver driver;
#BeforeTest(alwaysRun = true)
public void SetUp() {
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
login loginpage = PageFactory.initElements(driver, login.class);
driver = new FirefoxDriver();
driver.get("http://URL/");
HomePage.ClickbtnCookieWarning();
HomePage.ClickLoginLink();
loginpage.userLogin("aa#yahoo.com", "125", "Test");
}
You're getting NullPointerException because you're using WebDriver instance before initialising.
You need to Initialize WebDriver before using this instance as :-
driver = new FirefoxDriver();
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
Login loginpage =PageFactory.initElements(driver, login.class);
If you want to use WebDriver as singleton which returns single instance for all your test methods you can follow this answer which is exactly you want.
The problem is in each class you are creating new instance of driver. You just need to create one driver instance in you base class where you do your browser setup. Please refer Page Object Model. Once the Driver instance is created you need to use the same in all your classes. Or else it will throw NullPointerException because driver will not have any reference.

NoSuchWindowException: no such window: target window already closed

I am running a test using selenium WebDriver and getting following error:
org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=50.0.2661.102)
The code I wrote is:
#Test
public void LogIn_Page() throws InterruptedException {
driver.get(config.getApplicationUrl());
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("========== Login Test Start =========");
login.loginAction("username", "password");
}
public class LogIn_Page {
static WebDriver driver;
#FindBy(how = How.CLASS_NAME, using = "button-login")
public WebElement buttonLogin;
#FindBy(how = How.CSS, using = "input[class='is-text']")
public WebElement userName;
#FindBy(how = How.NAME, using = "j_password")
public WebElement passWord;
#FindBy(how = How.CSS, using = "button[class='login__button']")
public WebElement submit;
#FindBy(how = How.ID, using = "login-popup")
public WebElement loginPop;
public LogIn_Page(WebDriver driver) {
this.driver = driver;
}
public void loginAction(String UserName, String PassWord) {
buttonLogin.click();
userName.sendKeys(UserName);
passWord.sendKeys(PassWord);
submit.click();
}
}
The test I want to do:
Press the login button which creates a log in popup window
Enter email, password,
Press login.
The popup window appear, but the values of email and password not written (exception attached)

Categories

Resources