org.testng.TestNGException: Cannot instantiate class - java

I am receiving the following errors when I am trying to run my script
org.testng.TestNGException: Cannot instantiate class Caused by:
java.lang.reflect.InvocationTargetException Caused by:
java.lang.IllegalStateException: The path to the driver executable
must be set by the webdriver.ie.driver system property;
package EDRTermsPackge;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
public class ContactInformationTesting {
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#BeforeMethod
public void beforeMethod() {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
}
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
}

You should set your path to driver first and then instantiate IEDriver, you can't use new InternetExplorerDriver(); before System.setProperty("webdriver.ie.driver", exePath);
In your case you could do something like this (No need for #BeforeMethod to do just this simple property setup):
public class ContactInformationTesting {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
System.setProperty("webdriver.ie.driver", exePath);
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}

String exePath = "\Users\jj85274\Desktop\IEDriverServer.exe";
This line kind of hints as if you are trying to set the IEDriverServer binary from a network drive. Is that so ? I am not sure if network paths can be accessed by Java code directly.
I would suggest that instead of trying to add the IEDriverServer.exe path in your source code for every test, you might as well include this binary in your PATH variable. You can do this on windows by dropping this exe into one of the directories that is listed in the output of the below command
On Windows
echo %PATH%
On Non-Windows
echo $PATH

Related

Error while executing Selenium script in Eclipse IDE

I am getting the below error for the below script. I have all the jar files added to the library.
============================================================
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at webdriver/Demo.Sample.main(Sample.java:11)
=============================================================
package Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Seems like, you have imported the wrong jar files. Download the selenium-server-standalone-2.53.1.jar file from https://selenium-release.storage.googleapis.com/index.html?path=2.53/ and import it into an eclipse and remove other jar files.
Since you are trying to automate Firefox using Selenium WebDriver, you need to download Firefox executable binary file as well. Based on your browser version, you can get gecko driver from https://github.com/mozilla/geckodriver/
And add the below line in your code before the driver initialization and provide absolute path of Firefox executable binary file with name and extention :
System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable");
Below is your modified code :
package Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable\\geckodriver.exe"); // Provide your system path to the gecko driver with name and extension here
System.out.println("hello");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
I hope it helps...

Java - Get error when trying to login to a website with selenium webdriver (htmlunit)

I tried the below code with actual credentials for Indeed.com,
and I get an error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[#id="signin_email"]
I get a similar error when I use By.id instead of By.xpath, any idea what's going on?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
You need to enable java script, see the updated code.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
The login form is added via JS not regular HTML. I tried to disable JS in the browser and all I could see was:
<div id="container"></div>
This means that all you have to do is:
Enable JavaScript for your driver: driver.setJavascriptEnabled(true);
Wait until "signin_email" is rendered: driver.until(ExpectedConditions.presenceOfElementLocated(By.id("signin_email")))
The second bullet will give you stable test on different PCs. Sometimes, on weaker machines, the assertions may execute faster than the element was rendered by JS which leads to random failures.

The path to the driver executable must be set by the webdriver.gecko.driver issue in selenium webdriver

I'm trying to run the following sample snippet
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
//System.out.println("My new program");
}
}
When I run this code, getting the following error.
The path to the driver executable must be set by the webdriver.gecko.driver system property;
Firefox version is 48.0
Could anyone please help me to fix this issue.
If you are using windows follow the steps:
Right click my computer and select properties.
Click advanced settings->Environment variables.
Under System variable there should be variable named Path.
By the end of the path variable value add semi colon and then add specify your jecko driver's path. Example(C:\Jeckodriver).
Now compile the code. If it still throws exception then downgrade Firefox to 47.0.1.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test
{
public static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","Browser path.exe");
driver = new FirefoxDriver();
driver.get("http://www.google.com");
//System.out.println("My new program");
}
}
Download Gecko driver and extract to any folder. Specify gecko driver's path in path variable.

Getting error when run my first appium test

I am getting error in the eclipse when I run my first test in the eclipse using appium.
Following is my code:
package Appium;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.android.AndroidDriver;
//import org.openqa.selenium.remote.CapabilityType;
public class Appium {
WebDriver driver;
#BeforeClass
public void Appium() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Chrome");
capabilities.setCapability("VERSION", "5.1");
capabilities.setCapability("deviceName","Nexus 5");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.android.calculator2");
// This package name of your app (you can get it from apk info app)
capabilities.setCapability("appActivity","com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
//Create RemoteWebDriver instance and connect to the Appium server
//It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal() throws Exception {
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
//locate the edit box of the calculator by using By.tagName()
WebElement results=driver.findElement(By.tagName("EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
}
#AfterClass
public void teardown(){
//close the app
driver.quit();
}
}
When I run this code using testNg,I am getting following error.
FAILED CONFIGURATION: #BeforeClass Appium
java.lang.NoClassDefFoundError: org/json/JSONObject at
org.openqa.selenium.logging.profiler.HttpProfilerLogEntry.constructMessage(HttpProfilerLogEntry.java:36)
at
org.openqa.selenium.logging.profiler.HttpProfilerLogEntry.(HttpProfilerLogEntry.java:28)
at
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
at
org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242)
at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:128)
at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:155)
at Appium.Appium.Appium(Appium.java:47)
Make sure you've imported all the external jar packages properly in your project
As suggested in above answer, you may be missing google-gson jar in your project.
You will find the maven dependency on same page.

How can I execute Selenide in Chrome using ChromeDriver

I started using selenide (selenium wrapper api) and must say its a great tool but my only issue is its lack of documentation or usage examples online yet.
Any idea how to run your application coded in selenide in google-Chrome. I am using eclipse as IDE. I have added an environment variable "browser" with value chrome in my run configuration but when I run it picks up firefox.
My stack is
JDBC
Java
Selenide
Try this
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
You can find some documentation here.
The below code will help you launch the Chrome Browser with Selenide, and not with the selenium. It means you don't have to issue a close or quit command at the end of the iteration.
import static com.codeborne.selenide.CollectionCondition.size;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.*;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import com.codeborne.selenide.junit.ScreenShooter;
public class SimpleDateFormatClass {
#Rule
public ScreenShooter takeScreenshotSelenide = ScreenShooter.failedTests().succeededTests();
#Test
public void checkGoogleSearchResultsReturnValidResultNumberAndText() {
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver_2");
//Doesn't matter chrome or Chrome as this is case insensitive.
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
$(By.name("q")).setValue("Selenide").pressEnter();
// assure there are 10 results in the page
// static import shortcut, place the cursor on the method and press
// ctrl+shift+m/ cmd+shift+m
// $ -> driver.findElement, $$ -> driver.findElements
$$(".iris li.g").shouldHave(size(10));
$(".iris li.g").shouldHave(text("Selenide World!"));
}
}
This should help you even if you are running from the command prompt/ terminal, but if you want to exclusively pass on the Chrome from cmd you can use the "browser" parameter as below
-Dselenide.browser=chrome
You need to tell Selenide what browser to use. That can be done using Configuration properties:
import com.codeborne.selenide.Configuration;
public class Tests {
#Before
public void setBrowser() {
Configuration.browser = "chrome";
}
Remember: your webdriver should be placed on the standard path. For unix/linux it is: /usr/local/bin;
If your webdriver is located on a different path or renamed -- you need to set a system property with right path to the webdriver. For example:
Windows:
System.setProperty("webdriver.chrome.driver", "C:\\Program files\\chromedriver.exe");
Linux\Unix:
System.setProperty("webdriver.chrome.driver","/usr/share/chromedriver");
Make sure that your unix / linux chromedriver is executable. After this, you should have a fully working example (in my case, chromedriver is renamed and has version information):
import com.codeborne.selenide.*;
import org.openqa.selenium.*;
import org.junit.*;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
public class TestClass {
#Before
public void setBrowser(){
Configuration.browser = "chrome";
Configuration.browserSize = "1920x1080";
Configuration.holdBrowserOpen = true;
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver_2.33");
}
#Test
public void gotoGoogle(){
open("https://www.google.com");
WebElement searchBox = $(By.xpath("//input[#id='lst-ib']"));
$(searchBox).shouldBe(Condition.visible).setValue("How can I execute Selenide in Chrome using ChromeDriver").pressEnter();
WebElement firstResultLink = $(By.xpath("(//div[#class='rc']//a)[1]"));
$(firstResultLink).click();
System.out.println(getWebDriver().getCurrentUrl());
}
}
Another way is to use this command line switch with Maven:
mvn test -P chrome
It requires the Maven profiles in the pom.xml file such as are seen here:
https://github.com/selenide-examples/google
You can use System.setProperty("selenide.browser", "chrome"); for running in the chrome browser. If the same test you need to perform in safari just change the chrome to safari.
Eg:
System.setProperty("selenide.browser", "safari"); open("http://idemo.bspb.ru/");

Categories