Working on Selenium Webdriver and using Java. I'm getting error as The system cannot find the path specified
Code:
package test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class OEPR_DefaultTab{
private static Logger Log = Logger.getLogger(OEPR_DefaultTab.class.getName());
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();
Properties p= new Properties();
public Selenium selenium;
#BeforeTest
public void Login() throws Exception {
driver = new FirefoxDriver();
try {
p.load(new FileInputStream("C:/Login.txt"));
} catch (Exception e) {
e.getMessage();
}
String url=p.getProperty("url");
DOMConfigurator.configure("src/log4j.xml");
Log.info("______________________________________________________________");
Log.info("Initializing Selenium...");
selenium = new DefaultSelenium("localhost", 4444, "*firefox",url);
Thread.sleep(5000);
Log.info("Selenium instance started");
try {
p.load(new FileInputStream("C:/Login.txt"));
} catch (Exception e) {
e.getMessage();
}
Log.info("Accessing Stored uid,pwd from the stored text file");
String uid=p.getProperty("loginUsername");
String pwd=p.getProperty("loginPassword");
Log.info("Retrieved uid pwd from the text file");
try
{
driver.get("https://10.4.16.159/login");
}
catch(Exception e)
{
Reporter.log("network server is slow..check internet connection");
Log.info("Unable to open the website");
throw new Error("network server is slow..check internet connection");
}
performLogin(uid,pwd);
}
public void performLogin(String uid,String pwd) throws Exception
{
Log.info("Sign in to the OneReports website");
Thread.sleep(5000);
Log.info("Enter Username");
driver.findElement(By.id("loginUsername")).sendKeys(uid);
Log.info("Enter Password");
driver.findElement(By.id("loginPassword")).sendKeys(pwd);
//submit
Log.info("Submitting login details");
waitforElement(driver,120 , "//*[#id='submit']");
driver.findElement(By.id("submit")).submit();
Thread.sleep(6000);
Actions actions = new Actions(driver);
Log.info("Clicking on Reports link");
if(existsElement("reports")==true){
WebElement menuHoverLink = driver.findElement(By.id("reports"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
}
else{
Log.info("element not present");
System.out.println("element not present -- so it entered the else loop");
}
Log.info("Clicking on Extranet link");
if(existsElement("extranet")==true){
WebElement menuHoverLink = driver.findElement(By.id("extranet"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
}
else{
Log.info("element not present");
System.out.println("element not present -- so it entered the else loop");
}
Log.info("Clicking on PR link");
if(existsElement("ext-pr")==true){
WebElement menuHoverLink = driver.findElement(By.id("ext-pr"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
}
else{
Log.info("element not present");
System.out.println("element not present -- so it entered the else loop");
}
Log.info("Clicking on Overview and Evolution PR link");
if(existsElement("ext-pr-backlog-evolution")==true){
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("ext-pr-backlog-evolution") ));
//executor.executeScript("document.getElementById('ext-pr-backlog-evolution').style.display='block';");
//driver.findElement(By.id("ext-pr-backlog-evolution")).click();
// WebElement menuHoverLink = driver.findElement(By.id("ext-pr-backlog-evolution"));
//actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
}
else{
Log.info("element not present");
System.out.println("element not present -- so it entered the else loop");
}
}
//Filter selection-1
#Test()
public void Filterselection_1() throws Exception{
BufferedReader in = new BufferedReader(new FileReader("C:/FilerSection/visualization.txt"));\\ Here i'm getting error
String line;
line = in.readLine();
in.close();
String[] expectedDropDownItemsInArray = line.split("=")[1].split(",");
// Create expected list :: This will contain expected drop-down values
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
for(int i=0; i<expectedDropDownItemsInArray.length; i++)
expectedDropDownItems.add(expectedDropDownItemsInArray[i]);
// Create a webelement for the drop-down
WebElement visualizationDropDownElement = driver.findElement(By.id("visualizationId"));
// Instantiate Select class with the drop-down webelement
Select visualizationDropDown = new Select(visualizationDropDownElement);
// Retrieve all drop-down values and store in actual list
List<WebElement> valuesUnderVisualizationDropDown = visualizationDropDown.getOptions();
ArrayList<String> actualDropDownItems = new ArrayList<String>();
for(WebElement value : valuesUnderVisualizationDropDown){
actualDropDownItems.add(value.getText());
}
// Compare expected and actual list
for (int i = 0; i < actualDropDownItems.size(); i++) {
if (!expectedDropDownItems.get(i).equals(actualDropDownItems.get(i)))
System.out.println("Drop-down values are NOT in correct order");
}
}
private boolean existsElement(String id) {
try {
driver.findElement(By.id(id));
} catch (Exception e) {
System.out.println("id is not present ");
return false;
}
return true;
}
private void waitforElement(WebDriver driver2, int i, String string) {
// TODO Auto-generated method stub
}
#AfterTest
public void tearDown() throws Exception {
Log.info("Stopping Selenium...");
Log.info("______________________________________________________________");
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
Assert.fail(verificationErrorString);
}
}
}
Please check the code give me some solution.
The scenario which is present in the How to compare the drop down options is matching with the UI options in Selenium WebDriver?
For this scenario I'm trying to script. Please check the link as well.
If that's the exact text you are seeing this typically isn't a code issue - it means you need to update your PATH environment variable with the directory where java was installed.
Replace
BufferedReader in = new BufferedReader(new FileReader("C:/FilerSection/visualization.txt"));
with
BufferedReader in = new BufferedReader(new FileReader("C:\\FilerSection\\visualization.txt"));
This should help.
Related
I tried some selenium and it worked but stopped in the middle of the crawling process
these are codes:
import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Kream {
private static WebDriver driver;
public static final String WEB_DRIVER_ID = "webdriver.chrome.driver"; // 크롬 드라이버
public static final String WEB_DRIVER_PATH = "C://chromedriver.exe";
public static void main(String[] args) throws InterruptedException {
Kream krm = new Kream();
int rank = 0; // 순서 주려고 선언
System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH); // 운영체제 드라이버 설정
ChromeOptions options = new ChromeOptions(); // 옵션 쓰려고 객체화
options.addArguments("headless");
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(options);
try {
String url = "https://kream.co.kr/search?category_id=34&sort=popular&per_page=40";
driver.get(url);
List<WebElement> el = driver.findElements(By.className("search_result_item"));
var stTime = new Date().getTime(); //현재시간
while (new Date().getTime() < stTime + 15000) { //30초 동안 무한스크롤 지속
Thread.sleep(500); //리소스 초과 방지
//executeScript: 해당 페이지에 JavaScript 명령을 보내는 거
((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)", el);
for (WebElement element:el) {
System.out.println(++rank+". ");
System.out.print(element.findElement(By.tagName("img")).getAttribute("src"));
System.out.print("|"+element.findElement(By.className("brand")).getText());
System.out.print("|"+element.findElement(By.className("name")).getText());
System.out.print("|"+element.findElement(By.className("translated_name")).getText());
System.out.print("|"+element.findElement(By.className("amount")).getText());
System.out.print("|"+element.findElement(By.className("desc")).getText());
System.out.print("|"+element.findElement(By.className("express_mark")).getText());
System.out.println();
}
}
} finally {
driver.close();
driver.quit();
}
}
}
and return the error that says
onError
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394)
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426)
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
current versions:
chromedriver version:103.0.5060.24
chrome version: 103.0.5060.114
I already tried to match the both versions so mismatching wouldn't be the reason probably.
Thank you for the answer in advance.
I made changes to your script and this was working fine as expected, please check the below!
If still you encountered any issues, kindly share complete logs for issue resolution.
package com.selenium;
import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Kream {
private static WebDriver driver;
public static final String WEB_DRIVER_ID = "webdriver.chrome.driver"; // 크롬 드라이버
public static final String WEB_DRIVER_PATH = "C://chromedriver.exe";
public static void main(String[] args) throws InterruptedException {
Kream krm = new Kream();
int rank = 0; // 순서 주려고 선언
System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH); // 운영체제 드라이버 설정
ChromeOptions options = new ChromeOptions(); // 옵션 쓰려고 객체화
options.addArguments("headless");
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver();
try {
String url = "https://kream.co.kr/search?category_id=34&sort=popular&per_page=40";
driver.get(url);
List<WebElement> el = driver.findElements(By.className("search_result_item"));
var stTime = new Date().getTime(); //현재시간
while (new Date().getTime() < stTime + 15000) { //30초 동안 무한스크롤 지속
Thread.sleep(500); //리소스 초과 방지
//executeScript: 해당 페이지에 JavaScript 명령을 보내는 거
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)", el);
for (WebElement element : el) {
System.out.println(++rank + ". ");
System.out.print(element.findElement(By.tagName("img")).getAttribute("src"));
System.out.print("|" + element.findElement(By.className("brand")).getText());
System.out.print("|" + element.findElement(By.className("name")).getText());
System.out.print("|" + element.findElement(By.className("translated_name")).getText());
System.out.print("|" + element.findElement(By.className("amount")).getText());
System.out.print("|" + element.findElement(By.className("desc")).getText());
// System.out.print("|" + element.findElement(By.className("express_mark")).getText());
/*The above tag with class name as express_mark doesn't have any text attribute and hence, this is commented out!*/
System.out.println();
}
}
} finally {
driver.close();
driver.quit();
}
}
}
So basically; I found these 2 classes for creating a cookie and adding it to a session from some website because I don't know anything about cookies. I deleted some things from the code like "isSecured" because I thought it's unnecessary.
Creating the cookie is no problem but adding it to the session doesn't work, hopefully not due to the parts I deleted... But I really think they are unimportant.
Here is my whole class:
package indeed;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Cookies {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input = 0;
System.out.print("input 1 for readCookie or 2 for writeCookie: ");
input = sc.nextInt();
switch (input) {
case 1:
readCookie();
break;
case 2:
writeCooke();
break;
default:
System.out.println("no");
break;
}
}
public static void readCookie() {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get("https://secure.indeed.com/account/login?hl=de&continue=%2Faccount%2Fview%3Fhl%3Dde");
// Input Email id and Password If you are already Register
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email-input"))).sendKeys("example#email.com");
driver.findElement(By.id("login-password-input")).sendKeys("examplepass");
driver.findElement(By.id("login-submit-button")).click();
// create file named Cookies to store Login Information
File file = new File("Cookies.data");
try {
// Delete old file if exists
file.delete();
file.createNewFile();
FileWriter fileWrite = new FileWriter(file);
BufferedWriter Bwrite = new BufferedWriter(fileWrite);
// loop for getting the cookie information
// loop for getting the cookie information
for (Cookie ck : driver.manage().getCookies()) {
Bwrite.write((ck.getName() + ";" + ck.getValue()));
Bwrite.newLine();
}
Bwrite.close();
fileWrite.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void writeCooke() {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
driver = new FirefoxDriver();
try {
File file = new File("Cookies.data");
FileReader fileReader = new FileReader(file);
BufferedReader Buffreader = new BufferedReader(fileReader);
String strline;
while ((strline = Buffreader.readLine()) != null) {
StringTokenizer token = new StringTokenizer(strline, ";");
while (token.hasMoreTokens()) {
String name = token.nextToken();
String value = token.nextToken();
String domain = token.nextToken();
Cookie ck = new Cookie(name, value, domain);
System.out.println(ck);
driver.manage().addCookie(ck); // This will add the stored cookie to your current session
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
driver.get("https://secure.indeed.com/account/login?hl=de&continue=%2Faccount%2Fview%3Fhl%3Dde");
}
}
So I solved this particular problem myself by NOT using cookies but instead using the same instance of webdriver to avoid needing cookies in the first place
public static void switchToNewTab() {
openNewTab();
String subWindowHandler = null;
Set<String> handles = getDriver().getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()) {
subWindowHandler = iterator.next();
}
getDriver().switchTo().window(subWindowHandler);
}
public static void openNewTab() {
((JavascriptExecutor) getDriver()).executeScript("window.open('about:blank','_blank');");
}
public static void firstRunWebpage() {
getDriver().get("https://employers.indeed.com/p#post-job");
getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email-input"))).sendKeys("example#email.com");
getDriver().findElement(By.id("login-password-input")).sendKeys("examplepass");
//getDriver().findElement(By.xpath("//*[#id=\"label-login-rememberme-checkbox\"]")).click(); //typically already checked
getDriver().findElement(By.id("login-submit-button")).click();
}
public static void afterFirstRun() {
switchToNewTab();
getDriver().get("https://employers.indeed.com/p#post-job");
}
I am working with excel using selenium web driver where username and password is passed is taken from excel and passed to application,pass/fail status is written back to excel.During exceptions such as no element found etc the execution stops. How to continue execution from the point where it stopped in the excel. Following is my code:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;
public class Checkbox2 {
static WebDriver driver;
private static String filePath = "C:\\TEST DATA\\Users\\test.xlsx";
private static String sheetName = "Sheet1";
static File fl= new File(filePath);
public static void main(String[] args) throws InterruptedException, EncryptedDocumentException, InvalidFormatException
{
System.setProperty("webdriver.firefox.bin", "C:\\Users\\vijayab\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://qa-bnymellon.correctnet.com/bnymellon/release10/me.get?DPS.home");
// driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click();
try {
FileInputStream fis = new FileInputStream("C:\\Users\\vijayab\\Documents\\Work\\TEST DATA\\Users\\test.xlsx");
Workbook wb;
wb = WorkbookFactory.create(fis);
Sheet sheet = wb.getSheet("Sheet1");
for(int count=0;count<=sheet.getLastRowNum();count++)
{
Row row = sheet.getRow(count);
System.out.println("\n----------------------------");
System.out.println("Running test case " + count);
runTest(count, sheet, row.getCell(0).toString(),row.getCell(1).toString(),row,wb);
}
fis.close();
driver.close();// Closing the firefox driver instance
} catch (IOException e) {
System.out.println("Test data file not found");
}
}
public static void runTest(int count,Sheet sheet,String name,String mailid, Row row, Workbook wb) throws InterruptedException, InvalidFormatException, IOException
{
System.out.println("Inputing name: "+name+" and mailid: "+mailid);
driver.findElement(By.name("USERNAME")).sendKeys(name);
driver.findElement(By.name("PASSWORD")).sendKeys(mailid);
driver.findElement(By.name("SIGNIN")).click();
//driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
try{
if(driver.findElements(By.name("loginForm")).size() == 0){
System.out.println("Valid credentials"+count);
driver.findElement(By.name("DISCLAIMER")).click();
driver.findElement(By.id("ACCOUNTDOCUMENTS")).click();
driver.findElement(By.xpath("//*[contains(text(),'Account Profile')]")).click();
Thread.sleep(3000);
driver.switchTo().frame("FDX1");
driver.switchTo().frame("frame2-1");
Thread.sleep(4000);
driver.findElement(By.name("chk")).click();
driver.findElement(By.name("PROFILE_UPDATE")).click();
Alert alert = driver.switchTo().alert();
alert.accept();
driver.get("<logout url>");
int cellindex = 3;
WriteToFile.setExcelData(wb,filePath, sheetName, row.getRowNum(),cellindex, "PASS");
System.out.println("Inputted name: "+name+" and mailid: "+mailid);
Thread.sleep(2000);
}
else if(driver.findElements(By.name("loginForm")).size() == 0){
driver.findElement(By.name("loginForm")).isDisplayed();
System.out.println("Inputted name: "+name+" and mailid: "+mailid + "does not exist");
int cellindex = 3;
WriteToFile.setExcelData(wb,filePath, sheetName, row.getRowNum(),cellindex, "Fail");
return;
}
}
catch(UnreachableBrowserException e){
System.out.println("Exception occured");
}
}
}
There could be different ways to handle it. What i would suggest is below one.
Add another Catch block to catch Exceptions in method runTest. Return aBoolenvalue as false in case of exceptions. Based on the return type, decide on how to proceed further in you For Loop. I Guess this should help you solve the problem. Please let me know in case of any queries.
I created a class which has page objects defined in it. Then I am trying to use that class in a test class by using a constructor. However when I run the test class using JUnit I am getting a NullPointerException error.
Page_Objects Class:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
public class Page_Objects {
private WebDriver driver;
WebElement Username = driver.findElement(By.id("username"));
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
WebElement Password = driver.findElement(By.id("password"));
WebElement Login_Button = driver.findElement(By.id("Login"));
WebElement Logout_Button = driver.findElement(By.linkText("Logout"));
}
Test Class:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Salesforce_Test {
private WebDriver driver1;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
Page_Objects pageobjects = new Page_Objects();
#Before
public void setUp() throws Exception {
driver1 = new FirefoxDriver();
baseUrl = "some url for testing/";
driver1.navigate().to(baseUrl);
driver1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void Login_Logout() throws Exception {
pageobjects.Username.sendKeys("someusername");
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
pageobjects.Password.sendKeys("somepassword");
pageobjects.Login_Button.click();
pageobjects.Logout_Button.click();
}
#After
public void tearDown() throws Exception {
driver1.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver1.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver1.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver1.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
When I run the above class I get a null pointer exception.
java.lang.NullPointerException
at Page_Objects.<init>(Page_Objects.java:20)
at Salesforce_Test.<init>(Salesforce_Test.java:16)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
I see two issues here. First is that the Page_Objects class is called before you initiated the driver. That's why when you're calling
Page_Objects pageobjects = new Page_Objects();
you're getting a NullPointerException.
So you want to initiate the Page_Objects after you've initiated the driver.
Second, you need to pass it the instance of the driver you've initiated, because right now it has its own Private driver which isn't initiated and therefore it's null.
So in short:
Initiate Page_Objects after you've initiated driver (after the driver1 = new FirefoxDriver(); line)
Call it with the driver you've initiated: Page_Objects pageobjects = new Page_Objects(driver1 );
I am running my script and it terminates after 4 seconds. Have been stuck on this for hours, any help very much appreciated. Could it be that I may be using JUnit 3 code somewhere in a Junit 4 script? Just a thought. I am also calling a seperate PageObjects script, could this be the reason? Code is below:
package com.testcases;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import jxl.*;
import com.PageObjects.RegisterUserPageObject;
import java.io.File;
import jxl.write.*;
public class RegisterUser {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://newtours.demoaut.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testRegisterUser() throws Exception {
RegisterUserPageObject registerUserPage = PageFactory.initElements (driver, RegisterUserPageObject.class);
Workbook wb = Workbook.getWorkbook(new File("C:\\Users\\mce\\Documents\\Selenium\\Projects\\Mecury Tours\\Data1.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("C:\\Users\\mce\\Documents\\Selenium\\Projects\\Mecury Tours\\Data2.xls"),wb);
WritableSheet s = copy.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
driver.get(baseUrl + "/");
driver.findElement(By.xpath("//a[contains(text(),'Register \n here')]")).click();
driver.findElement(By.name("firstName")).sendKeys("Tester");
driver.findElement(By.name("lastName")).clear();
driver.findElement(By.name("lastName")).sendKeys("01");
driver.findElement(By.name("phone")).clear();
driver.findElement(By.name("phone")).sendKeys("010101010");
driver.findElement(By.id("userName")).clear();
driver.findElement(By.id("userName")).sendKeys("test#test.com");
driver.findElement(By.name("address1")).clear();
driver.findElement(By.name("address1")).sendKeys("1 Test Ave");
driver.findElement(By.name("city")).clear();
driver.findElement(By.name("city")).sendKeys("Dublin");
driver.findElement(By.name("state")).clear();
driver.findElement(By.name("state")).sendKeys("Leinster");
new Select(driver.findElement(By.name("country"))).selectByVisibleText("IRELAND");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(s.getWritableCell("A"+i).getContents());
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("confirmPassword")).clear();
driver.findElement(By.name("confirmPassword")).sendKeys("password");
driver.findElement(By.name("register")).click();
driver.findElement(By.linkText("Home")).click();
s.addCell(new Label(4,i,"Executed"));
}
copy.write();
copy.close();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}