Exception on running IE web Driver - java

I'm new in java, using it for automatic tests. Please help me what I'm doing wrong with this code?
public static WebDriver driver = null;
public static WebDriver getDriver() {
if (driver == null) {
File fileIE = new File("src//test/java/iedriver.exe");
System.setProperty("webdriver.ie.driver", fileIE.getAbsolutePath());
}
try {
driver = new InternetExplorerDriver();
}
catch (Exception e)
e.printStackTrace();
}

Try to add DesiredCapabilities to your code.
if (driver == null) {
File fileIE = new File("src//test/java/iedriver.exe");
System.setProperty("webdriver.ie.driver", fileIE.getAbsolutePath());
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
try {
driver = new InternetExplorerDriver(ieCapabilities);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
the DesiredCapabilities help to set properties for the WebDriver. A typical usecase would be to set the path for any type of the WebDriver if your local installation doesn't correspond to the default settings.
You can read about class DesiredCapabilities and about its' using here: DesiredCapabilities

Related

How to change download path during runtime using Selenium with chrome driver

I have 20 pages and each page have 2 testcases and each testcase download a number of files. I want to change the download directory for each test case at runtime.
Here is the 'TestBaseClass' code which downloading all the files in one particular folder from where I have to separate them as per category and place them into a particular folder.
There are 20 folders and each folder is having 2 subfolders 'ChapterLevel' & 'PracticeLevel' in which I do have to place it manually.
Is it possible to change the download directory by passing a variable during runtime?
My TestBaseClass code:
public static WebDriver driver;
public static void initialization() throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTTest");
// Adding cpabilities to ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
// Launching browser with desired capabilities
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
} catch (Exception e) {
// generic exception handling
e.printStackTrace();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
}
Here is my testcase:
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
#BeforeMethod
public void setUp() {
try {
initialization();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
#Test
public void chapterrLevelTest() throws Exception {
New.hoverTest();
New.clickBottomOptions();
New.chapterOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Chapter Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
#Test
public void practiceLevelTest() throws Exception {
New.hoverTest();
New.clickBottomOptions();
New.providerOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Practice Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
}
Suppose you want to specify download folder for each test method.
Add parameter for downloadPath in initialization in TestBaseClass.
Add parameter for downloadPath in setup in ANA_TC16_RiskAnalysisNewTest, remove the #BerforMethod annotation and update each test method to call setup in begin with desired downloadPath.
public class TestBaseClass {
public static void initialization(String downloadPath) throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", downloadPath);
...
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
// #BeforeMethod
public void setUp(String downloadPath) {
try {
initialization(downloadPath);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
#Test
public void chapterrLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\ChapterLevel");
New.hoverTest();
...
}
#Test
public void practiceLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\PracticeLevel");
New.hoverTest();
...
}
...

How to quit ChromeDriver after Invalid Argument Exception

Here is my current code:
options.addArguments("--user-data-dir=Profile");
try {
driver = new ChromeDriver(options);
}
catch (Exception org.openqa.selenium.InvalidArgumentException) {
driver.quit
}
InvalidArgumentException isn't throwing until the browser window has already opened, if I try quit this in the catch block it obviously prints a nullpointer exception because the driver is never fully initialised. How can I handle an InvalidArgumentException such as the user data dir being in-use whilst also closing the chromedriver window that has already opened.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.InvalidArgumentException;
WebDriver driver;
Boolean success;
ChromeOptions options = new ChromeOptions();
try {
options.addArguments("--user-data-dir=Profile");
success = true;
}
catch (InvalidArgumentException invArgEx) {
success = false;
}
if (success) {
driver = new ChromeDriver(options);
}

Semaphore doesn't works with PhantomJs DesiredCapabilities

When I run this code with commented //driver = new PhantomJSDriver(desiredCapabilities);, All works fine. I can see only two "PhantomJS" system process.
2 process But if I run this code with uncommented driver = new PhantomJSDriver(desiredCapabilities);, The amount "PhantomJS" system processes more than 2 : more than 2 ... Why?
PhantomJsDriverManager.getInstance().setup();
Semaphore s = new Semaphore(2);
for (int i = 0; i < 10; i++) {
s.acquire();
new Thread() {
public void run() {
PhantomJSDriver driver = new PhantomJSDriver();
try {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
System.out.println("Start: " + getName());
desiredCapabilities.setJavascriptEnabled(true);
driver = new PhantomJSDriver(desiredCapabilities);
driver.get("https://community.oracle.com/");
driver.quit();
System.out.println("Stop: " + getName());
} catch (Exception e) {
driver.quit();
} finally {
driver.quit();
s.release();
}
}
}.start();
}
}

Chromedriver doesn't opens link a new tab or window , instead the expected link is opened in current tab/window

This is the code i have tried to handle window but the url for opens in the tab of google.
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver =new ChromeDriver();
driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
try {
Thread.sleep(3000);
for(String windowHandle:driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}
In the iteration over the window handles you are switching to both of them. The last switch returns the focus to the first window and the link is getting opened there.
You should do the switch only to the new window
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
String firstWindowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
try {
Thread.sleep(3000);
for(String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(firstWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}
Driver is opening in same window, because for loop is switching old handle
Code probably should look like this below
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver =new ChromeDriver();
driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
String currentHandle = driver.getWindowHandle();
try {
Thread.sleep(3000);
for(String windowHandle:driver.getWindowHandles()) {
if(!currentHandle.equals(windowHandle)){
driver.switchTo().window(windowHandle);
break;
}
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}

Facing "nullpointer exception" in appium [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am learning appium and trying to call an object from one class to another and facing null pointer exception.
Below is my code :
public class TestCommons {
public AndroidDriver driver;
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void tearDown() {
driver.closeApp();
}
}
I wanted to use above class i.e "TestCommons" in other class. I want to use driver object.
Second class is below :
public class Day03 extends TestCommons {
TestCommons commons = new TestCommons();
#BeforeClass
public void beforeClass() {
commons.setUp();
}
#Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
#AfterClass(enabled = true)
public void afterClass() {
commons.tearDown();
}
}
Getting null pointer in second program #:
if(driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed();
Can anyone clarify me please.
You have one of two issues.
1) driver was not set properly in setUp(). If this is the case you probably got an exception. Check your logs to make sure that there isn't an exception there.
2) driver.findElement(By.id("com.vector.guru99:id/action_quiz")) is returning null. You can check this by setting a debug point and running evaluate expression on that call.
try this way:
public class TestCommons {
public static AndroidDriver driver;
#BeforeClass
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#AfterClass
public void tearDown() {
driver.closeApp();
}
}
public class Day03 extends TestCommons {
#Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
}

Categories

Resources