Following is my Methods code :
package testcases;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import pages.ResultsPage;
import utilities.ExcelReader;
public class methodDefinations extends base {
public void tearDown(){
driver.quit();
}
public void setUp(String browser) throws MalformedURLException {
System.out.println(browser);
DesiredCapabilities capabilities = null;
System.out.println("before driver==null")
if(browser.equals("firefox")){
System.out.println("ff");
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(Platform.ANY);
}
else if(browser.equals("chrome")){
System.out.println("ch");
capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.ANY);
}
System.out.println("inside driver==null");
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
System.out.println(capabilities);
String browserName = capabilities.getBrowserName();
String browserVersion = capabilities.getVersion();
System.out.println("Browser = " + browserName + " "+browserVersion);
System.out.println("before testsiteurl");
driver.get("http://keep.google.com");
System.out.println("after testsite url");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void doLogin()
{
System.out.println("after maximize");
driver.findElement(By.cssSelector("#Email")).sendKeys("abcd#gmail.com");
driver.findElement(By.cssSelector("#next")).click();
driver.findElement(By.cssSelector("#Passwd")).sendKeys("q1w2e3r4t5y6u7i8o9p0");
driver.findElement(By.cssSelector("#signIn")).click();
driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div[1]/div[7]")).sendKeys("example");
System.out.println("example typed");
}
}
Following is my Test Case :
package testcases;
import java.net.MalformedURLException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utilities.ExcelReader;
public class testCasess {
#Parameters("browser")
#Test
public void setup(String browser) throws MalformedURLException, InterruptedException {
methodDefinations md = new methodDefinations();
md.setUp(browser);
}
#Test
public void login()
{
methodDefinations md = new methodDefinations();
md.doLogin();
md.tearDown();
}
Following is my XML :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="tests" thread-count="2" >
<test name="LoginTest">
<parameter name = "browser" value= "firefox"></parameter>
<classes>
<class name="testcases.testCasess"/>
</classes>
</test> <!-- Test -->
<test name="LoginTest2">
<parameter name = "browser" value= "chrome"></parameter>
<classes>
<class name="testcases.testCasess"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Parallel = "tests" not working in Grid 2.
It basically opens both the browsers. But when it comes to typing username, it types username in firefox twice. Nothing is typed in Chrome.
I dont understand why this is happening.
Following is the Base class :
package testcases;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class base {
public WebDriver driver=null;
public static WebDriverWait wait;
public static Properties OR = new Properties();
public static Properties Config = new Properties();
public static Logger log = Logger.getLogger("devpinoyLogger");
public static FileInputStream fis;
public void tearDown()
{
driver.quit();
}
}
}
Based on your code, this can only happen if your driver is static. Basically, you should have two instances of driver to interact with them separately.
My Base class is as follows :
package base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
public class TestBase {
public static WebDriver driver;
public static Properties OR = new Properties();
public static Properties Config = new Properties();
//public static Logger log = Logger.get
public static FileInputStream fis;
#BeforeSuite
#Parameters({ "browser" })
public void setUp(String browser) throws MalformedURLException{
if(driver==null)
{
try {
fis = new FileInputStream(System.getProperty("user.dir")+"/src/test/resources/properties/OR.properties");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
OR.load(fis);
//log.debug("OR file loaded");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis = new FileInputStream(System.getProperty("user.dir")+"/src/test/resources/properties/Config.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Config.load(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("*******************");
driver = Browser.getDriver(browser);
driver.get(Config.getProperty("testsiteurl"));
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
driver.manage().window().maximize();
//WebDriverWait wait = new WebDriverWait(driver, 10L);
}
}
#AfterSuite
public void tearDown(){
System.out.println("Closing browser");
driver.quit();
}
}
Related
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.
import static org.junit.Assert.;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class excel {
WebDriver driver;
#Before public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\IVS Files\Selenium\Drivers\chromedriver v2.43\chromedriver.exe"");
driver.get("http://10.82.180.36/Common/Login.aspx");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5000,TimeUnit.MILLISECOND);
}
#After public void tearDown() throws Exception {
driver.close();
}
#Test public void test() throws
IOException, InterruptedException {
driver.findElement(By.id("body_txtUserID")).sendKeys("donhere");
driver.findElement(By.id("body_txtPassword")).
sendKeys("don#123");
driver.findElement(By.xpath("//[#id="body_btnLogin "]")).click();
driver.findElement(By.id("GeneralTabMenu_pnlCustomer_TopMenu")).click();
String path ="https://infosystechnologies-my.sharepoint.com/personal/doddi_suvarna_ad_infosys_com/Documents/Book1.xlsx";
FileInputStream file = new FileInputStream(path);
XSSFWorkbook book = new XSSFWorkbook(file);
XSSFSheet sheet1 = book.getSheetAt(0);
int rowcount = sheet1.getFirstRowNum();
System.out.println(rowcount);
for (int i = 0; i <= sheet1.getLastRowNum(); i++) {
double loanamount =sheet1.getRow(i).getCell(0).getNumericCellValue();
int month =(int) sheet1.getRow(i).getCell(1).
getNumericCellValue();
String la = String.valueOf(loanamount);
String m = String.valueOf(month);
Thread.sleep(5000);
WebElement name =driver.findElement(By.id("body_cph_Loans_ddlLoanName"));
Select name1 = new Select(name);
name1.selectByIndex(1);
Thread.sleep(5000);
driver.findElement(By.id("body_cph_Loans_txtReqLoanAmount")).sendKeys(la);
driver.findElement(By.id("body_cph_Loans_txtNoOfEMI")).sendKeys(m);
driver.findElement(By.id("body_cph_Loans_btnViewEMI")).click();
String abc =driver.findElement(By.id("body_cph_Loans_lblEMIAmountText")).getText();
driver.findElement(By.id("body_cph_Loans_btnReset")).click();
}
}
}
driver.get("http://10.82.180.36/Common/Login.aspx");
driver = new ChromeDriver();
You should first create object and then launch url...
driver = new ChromeDriver();
driver.get("http://10.82.180.36/Common/Login.aspx");
package day2practice;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
public class day2 {
ChromeDriver driver = new ChromeDriver();
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashok\\Desktop\\chromedriver.exe");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://live.guru99.com");
}
#Test
public void f() {
String actual = driver.getTitle();
String expected = "THIS IS DEMO SITE";
Assert.assertEquals(actual, expected, "page title is same");
}
#AfterMethod
public void close() {
driver.quit();
}
}
Don't instantiate ChromeDriver class before assigning the path to chromedriver.exe. Moved the instantiation code to beforeMethod.
try the following code:
package day2practice;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class day2 {
ChromeDriver driver;
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashok\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://live.guru99.com");
}
#Test
public void f() {
String actual = driver.getTitle();
String expected = "THIS IS DEMO SITE";
Assert.assertEquals(actual, expected, "page title is same");
}
#AfterMethod
public void close() {
driver.quit();
}
}
I'm trying to run a piece of code that I found online to run JUnit test in parallel but the only thing I've modified is the URL to hit my local host. Problem is I'm constantly getting the following error and even though I've setup selenium grid many times and know how to set paths etc. I don't know how to fix this one.
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver.
here's the code
package AutomationFrameWork;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized extends Parameterized {
private static class ThreadPoolScheduler implements RunnerScheduler {
private ExecutorService executor;
public ThreadPoolScheduler() {
String threads = System.getProperty("junit.parallel.threads", "16");
int numThreads = Integer.parseInt(threads);
executor = Executors.newFixedThreadPool(numThreads);
}
#Override
public void finished() {
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException exc) {
throw new RuntimeException(exc);
}
}
#Override
public void schedule(Runnable childStatement) {
executor.submit(childStatement);
}
}
public Parallelized(Class<?> klass) throws Throwable {
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
package AutomationFrameWork;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
#RunWith(Parallelized.class)
public class JUnitParallel {
private String platform;
private String browserName;
private String browserVersion;
private WebDriver driver;
#Parameterized.Parameters
public static LinkedList<String[]> getEnvironments() throws Exception {
LinkedList<String[]> env = new LinkedList<String[]>();
env.add(new String[]{Platform.WINDOWS.toString(), "chrome", "27"});
env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"});
env.add(new String[]{Platform.WINDOWS.toString(),"ie","11"});
env.add(new String[]{Platform.WINDOWS.toString(),"safari","5.1.7"});
//add more browsers here
return env;
}
public JUnitParallel(String platform, String browserName, String browserVersion) {
this.platform = platform;
this.browserName = browserName;
this.browserVersion = browserVersion;
}
#Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform", platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("build", "JUnit-Parallel");
capability.setCapability("InternetExplorerDriverServer.IE_ENSURE_CLEAN_SESSION",true);
System.setProperty("webdriver.ie.driver",
"C:\\Users\\path to driver\\IEDriverServer.exe");
driver = new RemoteWebDriver(
new URL("http://localhost:7777".concat("/wd/hub")),
capability
);
}
#Test
public void testSimple() throws Exception {
driver.get("http://www.google.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
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