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");
Related
I tried to automate the MakeMyTrip site ,I am getting a stale element reference error in line number 42 which i have highlighted .. I have tried all possible solutions..could u please help me with it ..
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import net.bytebuddy.asm.Advice.Return;
public class Dropdown {
#SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\arthi\\ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Actions act = new Actions(driver);
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
WebElement Fromcity = driver.findElement(By.id("fromCity"));
act.doubleClick(Fromcity).perform();
Thread.sleep(1000);
WebElement From = driver.findElement(By.xpath("//*[#placeholder='From']"));
act.doubleClick(From).perform();
From.sendKeys("chenn");
//List Fromcities=driver.findElements(By.xpath("//*[#id='react-autowhatever-1']/div[1]/ul/li/div/div[1]/p"));
List<WebElement> Fromcities = driver.findElements(By.xpath("//ul[#role='listbox']//li/div[1]/div[2]"));
for (WebElement Cities : Fromcities)
{
String Citi = Cities.getText();
**if (Citi.equals("MAA"))** {
try {
Cities.click();
} catch (StaleElementReferenceException ex) {
Fromcities = driver
.findElements(By.xpath("//*[#id='react-autowhatever-1']/div[1]/ul/li/div/div[1]/p"));
Cities.click();
}
}
}
//***to path
driver.findElement(By.xpath("//input[#placeholder='To']")).click();
WebElement ToPlace = driver.findElement(By.xpath("//input[#placeholder='To']"));
act.doubleClick(ToPlace).perform();
ToPlace.sendKeys("mum");
List<WebElement> ToCities = driver.findElements(By.xpath("//ul[#role='listbox']//li/div[1]/div[2]"));
for (WebElement To : ToCities)
{
if (To.getText().contains("BOM")) {
try {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", To);
} catch (StaleElementReferenceException e) {
ToCities = driver.findElements(By.xpath("//ul[#role='listbox']//li/div[1]/div[2]"));
To.click();
}
List<WebElement> Radiobutton = driver
.findElements(By.xpath("//ul[#class='specialFareNew']/li/div[2]/p"));
for (WebElement Radio : Radiobutton) {
if (Radio.getText().contains("Armed Forces Fares")) {
Radio.click();
System.out.println("got the expected output");
break;
}
}
}
}
}
}
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();
}
}
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();
}
}
I am getting the null pointer error in the below code. If you run the code it will execute all the code just before the code where i mentioned 'captureScreenshot()' function after this its not executing.
package rough;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestEmailAndScreenshot1 {
public static WebDriver dr;
public static String mailscreenshotpath;
public static void captureScreenshot(){
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int sec = cal.get(Calendar.SECOND);
int min = cal.get(Calendar.MINUTE);
int date = cal.get(Calendar.DATE);
int day = cal.get(Calendar.HOUR_OF_DAY);
File scrFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
try {
mailscreenshotpath = System.getProperty("user.dir")+"\\screenshot\\"+year+"_"+date+"_"+(month+1)+"_"+day+"_"+min+"_" +sec+".jpeg";
FileUtils.copyFile(scrFile, new File(mailscreenshotpath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
WebDriver dr=new FirefoxDriver();
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
dr.get("http://gmail.com");
dr.findElement(By.id("Email")).sendKeys("vijenderchhoker92");
captureScreenshot();
System.out.println(" Screenshot taken successfully....");
}
}
When you do
WebDriver dr = new FirefoxDriver();
In main you hide the public static WebDriver dr; variable, so in captureScreenshot() method it's still null and you get the error trying to use it. Change to
public static void main(String[] args) {
dr = new FirefoxDriver();
dr.manage().window().maximize();
//...
}
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();
}
}