Following is the main Code:
private WebDriver driver = null;
try
{
System.setProperty("webdriver.chrome.driver",
"C:\\Jars\\chromedriver.exe");
driver = new ChromeDriver();
System.out.println("after chrome");
}
catch(Exception e){
System.out.println("error");
e.printStackTrace();
}
When the above code is executed as stand alone java, the driver was initialized and I was able to use for automation.
But when above code is packaged into a jar file,
print statement after driver = new ChromeDriver(); is not called.
Note: chromedriver.exe is outside the jar file
Could anyone help me understand this problem
Too silly of me!. I missed http client jar files while building. This created the problem
I guess you are fogot to upload jars.. Add them
Related
Executing simple class
Enter code here
package lesson1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Brf {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
}
Getting the error:
Error on simple class in selenium webdrive:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:335)
You are getting this error because you haven't set the path of gecko driver in your code.
Please set the path of gecko driver before creating a new instance of Firefox driver.
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
Need to add driver path, if you are using selenium jars above 3.0 , if you are using selenium jars below 3.0 your code is good to go, but i guess you are using selenium jars above 3.0
Public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
The cause of the error is that you don't have a proper setup for detection of a GeckoDriver file named "geckodriver.exe" (this prevents you from being able to open Firefox with your program). In case you don't have such a file, you can download one h̲e̲r̲e̲ (I suggest getting the latest version).
With such a file, you can carry out one of the following two procedures to rectify your problem:
Method 1
Add the path of the folder containing the "geckodriver.exe" file to your PATH environment variable value (instructions on how to modify this variable value can be seen h̲e̲r̲e̲).
Method 2
Prepend the body of your main method with the following statement (where GECKODRIVER_PATH represents the path of the "geckodriver.exe" file):
System.setProperty("webdriver.gecko.driver", "GECKODRIVER_PATH");
Set the gecko driver path which is initialise the firefox driver.
Here is my code:
package Basics;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class invokegoogle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("Webdriver.chrome.driver", "C:\\Users\\sravani\\Desktop.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://qaclickacademy.com");
}
}
Getting the following errors:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at Basics.invokegoogle.main(invokegoogle.java:12)
Any help is highly appreciated. Thanks in advance
Assuming the chromedriver.exe is stored at your desktop, you need to make a couple of changes as follows:
You need to replace the uppercase W with lower case w within Webdriver.chrome.driver.
As you are on windows system, presuming chromedriver.exe is placed in your desktop ypu need to append the WebDriver variant name along with the extension within the absolute path of the WebDriver.
Effectively the line of code will be:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\sravani\\Desktop\\chromedriver.exe");
Note: webdriver.chrome.driver needs to start with lowercase letter.
Once you download chrome driver into your system, after extracting it (unzipping it) into the folder, it looks like you have directly copied the folder path "Downloads/chromedriver_win32.exe", instead using full path as below mentioned
Open the folder (chromedriver_win32.exe), then you will see "chromedriver.exe" as .exe file, and use this path instead and it looks like this
System.setProperty("webdriver.chrome.driver", C:\Downloads\chromedriver_win32\chromedriver.exe");
This will work
Add your chrome driver to java resource folder
Add the below mentioned code will work
System.setProperty("webdriver.chrome.driver", Objects.requireNonNull(getClass().getClassLoader().getResource("drivers/chromedriver.exe")).getFile() );
i also got same issue then searched so many answer and apply into my code but result showing null
then after i realized that i had declare static variable on above of class
looks like
private static WebDriver driver = new ChromeDriver();
then i rewrite code on under void main class like
WebDriver driver = new ChromeDriver();
now my code running works fine
you can also try like this or let me know so i can help you.
I want to write couple of tests for a web page with Selenium and jUnit4, but I can't figure out how to make Firefox open the URL I need. Without System.setProperty(...) I'm getting Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. and browser never opens. Yet if I implement it the browser does open on a default start "new page", but the line driver = new FirefoxDriver(); and further never executes.
Below is the simplest code version of what I'm trying to achieve:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
static String URL = "http://www.google.com";
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//Following code never executes
driver = new FirefoxDriver();
//I'm not sure if this is how I'm supposed to open URL, but I never had this code executed.
driver.get(URL);
driver.quit();
}
}
UPDATED:
These links were helpful to solve the problem of proper geckodriver installation.
https://github.com/mozilla/geckodriver/releases
http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
Actually you need to set geckodriver.exe path instead of firefox.exe in this
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
So just replace geckodriver.exe path and try
I have added following jars in my projects build path:
java-client-2.0.0 from http://appium.io/downloads.html >> Appium Client libraries >> Java
selenium-java-2.43.1
selenium-java-2.43.1-srcs
selenium-server-standalone-2.43.1
and here's my code:
public class SampleApp{
WebDriver dr;
#Test
public void testApp() throws MalformedURLException, InterruptedException {
String apkpath = "D:\\apkdump\\sampleapp.apk";
File app = new File (apkpath);
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
capabilities.setCapability("deviceName","TestADB18");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("app",app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.test");
capabilities.setCapability("appActivity", "com.sampleapp.Main");
dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
dr.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
}
I am getting red line below new AppiumDriver which says that Cannot instantiate the type AppiumDriver. Now if remove all selenium jars the error disappears but then I can't resolve errors with webdriver.What is the conflict between jars?
I saw similar question here but that could run the code and was getting Null pointer exception but in my case I cant even run it, it is giving run on saving the code. Secondly the answer has been posted without using AppiumDriver
You don't need to downgrade or anything. There is a design change in the Java Client version 2.0.0 as they mention on their site:
AppiumDriver is now an abstract class, use IOSDriver and AndroidDriver which both extend it.
So, just change your driver line to be:
dr = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
Hope that helps...
This error can be fixed by downgrading the Appium Client(see step 1 in my question) from latest to java-client-1.5.0. You can keep rest of the jars to latest.
Downgraded version of Appium Client can be downloaded from here http://mvnrepository.com/artifact/io.appium/java-client/1.5.0
WebDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),getDesiredCapabilities("192.21.168.56:5555"));
use this. and import:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
dr=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
proper URL to be established
for appium version 1.7 use capability
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
while for v1.8.1
its not needed
When I connect to the OracleDriver in the application, everything is fine. But when I want to connect to run the JUnit Tests, I got a ClassNotFoundException. And I do exactly the same!
I have the ojbc added to the library and the testlibrary.
public JDBCDataStorage(boolean production) throws DataStorageException {
this.production = production;
try {
rb = (PropertyResourceBundle) PropertyResourceBundle.getBundle("app.control.database.JDBCconfig");
Class.forName(rb.getString("driver"));
} catch (ClassNotFoundException e) {
throw new DataStorageException("Something went wrong in new JDBCDataStorage()" + ": " + e.getMessage());
}
DriverManager.setLoginTimeout(3);
}
Check for two things
That rb.getString("driver") actually returns the FQCN of your driver.
That the driver JAR is in classpath of your test application
Try adding the Oracle JDBC driver jarfile to the classpath of the JUnit test. If you're running the unit test in Eclipse, add the driver jarfile to the User Entries in the Run Configuration of the JUnit test.