There is the Search Class where I made a method to do amazon search, and the Main Class calls the Method searchFor()
But I keep getting the error
Exception in thread "main" java.lang.NullPointerException
package Project1;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Search {
#FindBy(id = "twotabsearchtextbox")
WebElement search_box;
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}
And this is the Main Class
package Project1;
public class Main {
public static void main(String[] args) {
Search s1 = new Search();
s1.searchFor("gaming laptop");
}
}
Please refer below solution:
Main class
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("Your url ");
Search s1 = new Search(driver);
s1.searchFor("gaming laptop");
}
}
Search class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Search {
#FindBy(id = "twotabsearchtextbox")
WebElement search_box;
WebDriver driver;
public Search(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}
Related
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumWeddriver {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\91996\\Downloads\\chromedriver_win32");
WebDriver driver= new ChromeDriver();
driver.get("https://demo.guru99.com/selenium/newtours/");
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumWeddriver {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\91996\\Downloads\\chromedriver_win32.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://demo.guru99.com/selenium/newtours/");
}
}
You forgot to add chromedriver_win32.exe .exe extension.
Code trials:
package Pages;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
public class pageBase {
public static WebDriver driver;
public pageBase(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public static void AcceptAlert() {
Alert alert = driver.switchTo().alert();
alert.accept();
}
}
Test page:
public void SignUp(String user_name,String password) throws InterruptedException {
Set_UserName(user_name);
Set_Password(password);
click_SignupBtn();
Thread.sleep(3000);
AcceptAlert();
}
Error:
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.switchTo()" because "Pages.pageBase.driver" is null
In the pageBase pageobject as you are casting the WebDriver instance you need to remove the keywords:
public
static
Effectively, instead of:
public static WebDriver driver;
your line of code will be:
WebDriver driver;
References
You can find a couple of relevant detailed discussions in:
How to wait for invisibility of an element through PageFactory using Selenium and Java
I am trying to pass the webElement name to another class for Webdriver operations.I am using the pagefactory model.
I want to print the name of the webelement variable as well in another class.
The below is the code I have.
Class A:
Class A{
#FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
public WebElement exampleTab;
}
Class B:
class B{
public static void Click(WebElement objName) throws Exception
{
objName.click();
System.out.println("Clicked on"+ objName);
}
}
Desired Output:
Clicked on exampleTab
Actual Output:
Clicked on com.sun.proxy.$Proxy14
You can do that using below code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
class A {
public WebDriver driver;
#FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
public WebElement exampleTab;
public void initElements(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
public class B {
public static void main(String r[]) {
A a = new A();
System.setProperty("webdriver.chrome.driver",
"D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\chromedriver-2.35.exe");
WebDriver driver = new ChromeDriver();
a.initElements(driver); // instantiating class A elements
driver.navigate().to("url");
driver.manage().window().maximize();
Click(a.exampleTab);
}
public static void Click(WebElement objName) throws Exception {
objName.click();
System.out.println("Clicked on" + objName);
}
}
I have 2 classes
1.BROWSER--- This class has methods for loading browser so that i can call in my every test case
2.LOCATORS --This class contains methods for storing all webelements
3.NEW TEST-This is my test case, in which i have called "browser" and "locators" class...
Below is my Browser class
BROWSER CLASS
package TestProject.TestProject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Browser {
WebDriver driver;
public Browser (WebDriver driver) {
this.driver = driver;
}
public WebDriver GetBrowser()
{
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
driver = new ChromeDriver();
String baseurl = "https:\\live.guru99.com\\index.php\\";
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
}
Below is my Locator class
package TestProject.TestProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Locators {
WebDriver driver;
//Locators
By mobile = By.xpath("//a[contains(.,'Mobile')]");
public Locators (WebDriver driver){
this.driver = driver;
}
public void mobile()
{
driver.findElement(mobile).click();
}
}
MY TEST CASE
package TestProject.TestProject;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
public class NewTest {
WebDriver driver;
#BeforeTest
public void beforeTest() {
Browser load = new Browser(driver);
driver =load.GetBrowser();
}
#Test
public void VerifyMobile() {
Locators mobilemenu = new Locators(driver);
mobilemenu.mobile();
}
#AfterTest
public void afterTest() {
}
}
You get NullPointerException because you are using non-initialized WebDriver
Below code gives you trouble. You are passing null driver to Browser class and... Then you probably do something with it but you did not return any initialized WebDriver
public class NewTest {
WebDriver driver;
#BeforeTest
public void beforeTest() {
Browser load = new Browser(driver);
load.GetBrowser();
}
Try this:
public WebDriver GetBrowser() {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
driver = new ChromeDriver();
String baseurl = "https:\\live.guru99.com\\index.php\\";
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
You will return initialized WebDriver to your test like this:
driver = load.GetBrowser();
This is bound to give a NullPointerException as the WebDriver object in #BeforeTest method is not yet initialized and is thus Null.
Browser load = new Browser(driver);
Here, the driver object is not yet initialized.
Advice:
Instead of calling the GetBrowser() method, initialize the WebDriver object in the Browser class constructor, and inherit the Browser class, and use the WebDriver object as and when required.
I have a question with regards to calling multiple method using JUNIT. This is my test
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class test {
private WebDriver _driver;
#Test
public void FFconfiguration() throws Exception {
System.out.println("Running FF");
_driver = new FirefoxDriver();
_driver.get("URL");
login();
setup();
_driver.quit();
}
public void login1()
{
}
public void setup()
{
}
}
My question is: Can I call both login() and setup() within the method FFConfiguration? If not what's the alternate solution...............
Yes, absolutely, you can do it. You can have tests like this:
#Test
public void testBuyingProcess(){
ShoppingUI shoppingPage = new ShoppingUI();
shoppingPage.login();
Assert.assertEquals(shoppingPage.getTitle(),"Welcome");
//....
}
And fill in the methods elsewhere even in different class. Few examples of methods used above:
public class ShoppingUI{
private WebDriver driver
public ShoppingUI(){
driver = new FirefoxDriver();
driver.get("http://my-test-site.com/buy-buy-buy.html");
}
public String getTitle(){
return driver.getTitle();
}
}