Grid - Not able to run the multiple instance When we set both 'RemoteWebDriver' and 'WebDriver' as static. it returns null value - selenium-grid

When we call this Driver() method in Another class ex: 'main.TestNG.Driver()'. This 'Driver()' method is calling 'getDriver()' method
and getDriver() method is returns null value insteads of remote value. due to this issue i can't access those webdriver in another class.
We are using Keyword driven approach, we want to run this multiple script using grid approach in multiple nodes (instance)
When we set both 'RemoteWebDriver' and 'WebDriver' as static only 1 instance is execute
How to resolve the null ?
package main;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.AfterClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import Utility.AppProperties;
import Utility.ExecLogger;
public class TestNG extends AppProperties{
public ThreadLocal<RemoteWebDriver> threadDriver = new ThreadLocal<RemoteWebDriver>();
public WebDriver driver;
#Parameters({"browser"})
#BeforeClass
public void setup(String browser) throws MalformedURLException, InterruptedException {
DesiredCapabilities capability=new DesiredCapabilities();
if(browser.equalsIgnoreCase("firefox")){
System.out.println("firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
if(browser.equalsIgnoreCase("iexplore")){
System.setProperty("webdriver.iexplore.driver", "..........\\iexplore.exe");
System.out.println("iexplore");
capability= DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
}
if(browser.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "..........\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(Arrays.asList("--test-type"));
capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setCapability("chrome.switches", Arrays.asList("--incognito"));
capability.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
}`enter code here`
threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability));
Driver();
}
public RemoteWebDriver getDriver() {
// TODO Auto-generated method stub
return threadDriver.get();
}
public WebDriver Driver(){
try {
loadProperties();
} catch (IOException e) {
// TODO Auto-generated catch block`
e.printStackTrace();
}
driver = getDriver();
driver.get(getUrl());
driver.manage().window().maximize();
return driver;
}
}
When we call this Driver() method in Another class ex: 'main.TestNG.Driver()'. This 'Driver()' method is calling 'getDriver()' method
and getDriver() method is returns null value insteads of remote value. due to this issue i can't access those webdriver in another class.
We are using Keyword driven approach, we want to run this multiple script using grid approach in multiple nodes (instance)
When we set both 'RemoteWebDriver' and 'WebDriver' as static only 1 instance is execute
How to resolve the null ?

Related

NUll Pointer Exception using getTilte(); Selenium with Java

Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Login {
WebDriver driver ;
By userNameTextBox = By.name("email");
By passwordTestBox = By.id("password");
By loginButton = By.id("signInButton");
String titleText = driver.getTitle();
public Login(WebDriver driver) {
this.driver=driver;
}
public void setUserName() {
driver.findElement(userNameTextBox).sendKeys("v-tobias.rivera#shutterfly.com");
}
public void setPassword() {
driver.findElement(passwordTestBox).sendKeys("Indecomm1");
}
public void clickLogin() {
driver.findElement(loginButton).click();;
}
}
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
//import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import main.Login;
public class LoginTest {
WebDriver driver;
Login objLogin;
#BeforeMethod
public void beforeTest() {
System.setProperty("chromedriver", "/Users/tobiasriveramonge/eclipse-
workspaceAutomation/seleniumAutomation");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://accounts.shutterfly.com/?
redirectUri=https%3A%2F%2Fwww.shutterfly.com%2F&cid=&brand=SFLY&theme=SFLY");
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
element.isDisplayed();
}
#Test
public void Test() {
objLogin = new Login(driver);
//String title = objLogin.getTitle();
// System.out.println(title);
//Assert.assertTrue(loginPageTitle.toLowerCase().contains(" "));
objLogin.setUserName();
objLogin.setPassword();
objLogin.clickLogin();
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
I would like to know why am I getting this exception.
Hi, Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.
You did not initialize the driver object.
That's why referencing to that not initialized object throws the null pointer exception.
You only declared the driver object type by
WebDriver driver;
but never initialized it.

Selenium webDriver test suite [duplicate]

I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class NewTesting {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() {
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more?
I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!
EDIT:
I now have this:
public class NewTest {
private WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
It does open the website now but I'm getting a nullpointer exception now:
FAILED CONFIGURATION: #AfterTest tearDown
java.lang.NullPointerException
at NewTest.tearDown(NewTest.java:21)
Replace this set of imports:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
With:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.
Your effective code block will be:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class NewTesting {
WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
If you're on windows, this previous question may be some help to you.
It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

getWindowHandle() throws error message "Nullpointerexception"

this is a simple selenium function that is trying to get a windowhandle.
right at that statement it throws" Nullpointerexception"
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandles {
WebDriver achromeDriver;
String abaseUrl;
public void setUp() throws Exception {
abaseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();;
System.out.println("setup completed");
}
#Test
public void test() {
try{
String aparentwindowHandle = achromeDriver.getWindowHandle();
System.out.println("the parent window handle is "+ aparentwindowHandle);
WebElement aopenwindowelementbutton = achromeDriver.findElement(By.id("openwindow"));
aopenwindowelementbutton.click();
String achildwindowhandle = achromeDriver.getWindowHandle();
System.out.println("the child window handle is: " + achildwindowhandle);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
A bit more details about your usecase and your observation would have helped us to debug the issue in a better way. However, it seems as you are using the junit framework and in-absence of any of the annotations the setUp() method is never executed.
As the test() method is annotated with #Test the program reaches there with achromeDriver as Null
Solution
A quick solution would be to add #Before annotation to setUp() method as follows:
#Before
public void setUp() throws Exception {
abaseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();;
System.out.println("setup completed");
}

In my Junit test case driver.findElement(By.cssSelector) not being executed

For the Junit Test case, I am trying to open a browser, navigate to my site and the enter an email in an field. Although all my commands are correct, I cant understand why does it specifically stops and shows error for line 33 i.e. driver.findElement(By.cssSelector)
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasicActions {
WebDriver driver;
String BaseUrl;
#Before
public void setUp() throws Exception {
//System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new FirefoxDriver();
BaseUrl = "https://www.flock.co/in/indexd/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get(BaseUrl);
System.out.println("opening the base url");
driver.findElement(By.xpath("//div[#id='main-area']//input[#type='email']")).clear();
driver.findElement(By.cssSelector("._g-s-input>input")).sendKeys("testing#mailinator.com");
System.out.println("Entering a valid email id");
driver.findElement(By.xpath("//div[#id='main-area']/div[2]/div[2]//button[#class ='_g-s-button']")).click();
System.out.println("Redirecting to web.flock.co");
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
Appropriate syntax to find element by css class is :
driver.findElement(By.cssSelector("input._g-s-input"));
I am assuming '_g-s-input' is your css class name, if not so, please replace it with appropriate css class name.

Can any one help me , my Test Ng annotations are not running .In the Below code my #before class only running but not other

In the below code i am trying to automate gmail by data driven frame work using TestNg , but my code #Before annotation only executing not other two .Please healp me.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class LoginData {
WebDriver driver;
#Test
public void login() {
driver.findElement(By.linkText("Sign in")).click();
System.out.println("hello");
}
#BeforeTest
public void beforeTest() throws Exception {
WebDriver driver=new FirefoxDriver();
driver.get("https://www.gmail.com/intl/en/mail/help/about.html");
Thread.sleep(2000);
}
#AfterTest
public void fterTest() {
driver.close();
}
}
Since you have declared WebDriver driver already;
remove Webdriver from WebDriver driver==new FirefoxDriver();
I tried below and worked for me.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class LoginData1 {
WebDriver driver;
#Test
public void login() {
driver.findElement(By.linkText("Sign in")).click();
System.out.println("hello");
}
#BeforeTest
public void beforeTest() throws Exception {
driver=new FirefoxDriver();
driver.get("https://www.gmail.com/intl/en/mail/help/about.html");
Thread.sleep(2000);
}
#AfterTest
public void fterTest() {
driver.close();
}
}
Accept if it works for you or let me know if this does not work...
Thanks