Selenium code in java is not opening the browser - java

I am new in selenium and trying to open https://google.co.in in the chrome browser through selenium (below is the code). But I am not able to see the chrome browser after running this code. Could someone tell me that what's wrong with this code.
Here is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
System.out.println("Loading...");
WebDriver driver = new ChromeDriver();
driver.get("http://google.co/in");
String appTitle = driver.getTitle();
System.out.println("Application title is :: "+appTitle);
driver.quit();
}
}
And the output is...
Loading...

Download chromedriver from this link : http://chromedriver.storage.googleapis.com/2.24/chromedriver_win32.zip , unzip it & put chromedriver.exe in "E:\Application" and provide path to chromedriver in System.setProperty("webdriver.chrome.driver", "E:\\Application\\chromedriver.exe");

System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
Here E:\Application\chrome.exe is not your chrome driver.
Download the chrome driver of the version you need in your application.
Latest Release: ChromeDriver 2.24
Once you have the chrome driver , specify its location via the webdriver.chrome.driver system property (see sample below)
#Test
public void testGoogleSearch() {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}

You can use a following library
webdrivermanager
after using this, you don't need to download a driver for the specific browser.It will automatically download driver for you and setup.
In order to use WebDriverManager in a Maven project, first add the following dependency to your pom.xml:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.4.10</version>
</dependency>
Then you can let WebDriverManager to do manage WebDriver binaries for your application/test. Take a look to this JUnit example which uses Chrome with Selenium WebDriver:
public class ChromeTest {
protected WebDriver driver;
#BeforeClass
public static void setupClass() {
ChromeDriverManager.getInstance().setup();
}
#Before
public void setupTest() {
driver = new ChromeDriver();
}
#After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
#Test
public void test() {
// Using Selenium WebDriver to carry out automated web testing
}
}
Notice that simple adding ChromeDriverManager.getInstance().setup(); WebDriverManager does magic for you:
It checks the latest version of the WebDriver binary file
It downloads the binary WebDriver if it is not present in your system
It exports the required Java variable by Selenium WebDriver
So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, or Marionette as follows:
ChromeDriverManager.getInstance().setup();
InternetExplorerDriverManager.getInstance().setup();
OperaDriverManager.getInstance().setup();
EdgeDriverManager.getInstance().setup();
PhantomJsDriverManager.getInstance().setup();
MarionetteDriverManager.getInstance().setup();

Related

Could not able to launch chrome browser using the selenium webdriver

import org.openqa.selenium.chrome.ChromeDriver;
public class launch {
public static void main(String[] args) {
// TODO Auto-generated method stub
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
}
}
Error:
Could not find or load main class org.openqa.grid.selenium.GridLauncherV3
In the Launch class you are directly creating the object of ChromeDriver class without giving the chrome driver path.
So, before creating the ChromeDriver object you need to add:
System.setProperty("webdriver.chrome.driver","path of chromedriver.exe file");
If you have downloaded the chrome driver exe file then you need to pass that path.
After this create the object of ChromeDriver class.
You need to set the system property webdriver.chrome.driver as follows:
System.setProperty("webdriver.chrome.driver", '/path/to/chromedriver');
Additionally, instead of using the ChromeDriver use the WebDriver interface as follows:
WebDriver driver = new ChromeDriver();

Appium tests are not performed on my Android device

I am learning automation testing with Appium(Lastest 1.8.2)-Mobile Automation Testing from Scratch course on Udemy.
I am trying with real and virtual devices and I can launch the application, but my operation(.click) is not working.
I am working on IntelliJ IDEA Community 2019.2.
Appium version: v1.14.1
I created Java Project with Maven Module
JAR:
commons-lang3-3.0, client-combined-3.141.59, java-client, selenium-java
My code:
BASE CLASS:
public class base {
public static AndroidDriver Capabilities() throws MalformedURLException {
File f = new File("src");
File fs = new File(f, "ApiDemos-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Galaxy Tab S2");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,"UiAutomator2");
cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
return driver;
}
}
BASICS CLASS:
public class basics extends base {
public static void main(String[] args) throws MalformedURLException {
AndroidDriver<AndroidElement> driver = Capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Preference']")).click();
driver.findElementByXPath("//android.widget.TextView[#text='3. Preference dependencies']").click();
driver.findElementById("android:id/checkbox").click();
}
}
The problem is that I am receiving (probably) good exit code in Appium:
Got response with status 200
But I cannot see that test is performed on my device with Android.
Did I omit something?
You don't need that many dependencies, it is quite enough to have io.appium.java-client only, Appium Java Client includes Selenium libraries and guaranteed to work only with the version which it's linked with
So
First of all try to remove all the dependencies apart from the Appium Java Client.
Explicitly set up the following Desired Capabilities:
MobileCapabilityType.PLATFORM_NAME with the value of android
AndroidMobileCapabilityType.APP_PACKAGE with the value of your application package
AndroidMobileCapabilityType.APP_ACTIVITY with the value of your application activity
MobileCapabilityType.AUTOMATION_NAME should be uiautomator2
You can check out Appium -> Code Examples -> Java for comprehensive information and sample code repository which you can use as the "skeleton" for your test

Insecure Connection error Firefox v 53.0

I am using selenium 3.4.0 , firefox version 53.0 and gecko driver 0.16.1 , java compiler 1.7.
For some sites insecure connection error is displayed.
I have used firefox profile object as follow but still it's not resolving:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
To work with Selenium 3.4.0 with Mozilla Firefox browser 53.x you need to download the latest geckodriver from here. Save it in your machine & provide the absolute path of the geckodriver.
Create a new Firefox profile manually by the name debanjan and use the AcceptUntrustedCertificates & setAssumeUntrustedCertificateIssuer options.
This code executes fine with some simple tweak to your own code.
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("network.proxy.type", 1);
testprofile.setPreference("network.proxy.http", "localhost");
testprofile.setPreference("network.proxy.http_port", 3128);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
Let me know if this helps you.
Try using firefox below 48 version. You won't face any problem or include below code in your existing code:
System.setProperty("webdriver.firefox.bin" ,"C:/Users/siddhesh.kalgaonkar/AppData/Local/Mozilla Firefox/firefox.exe");
It should solve your problem because this is what I use for current firefox version.
Use Firefox 54.0 64bit, Selenium v3.4.0, jcommender v1.7, TestNG v6.9.9, Java v8.0, Gecko driver v0.17.0
Use below code-
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
public class AppUrl {
public static WebDriver driver;
public static final String url = "https://10.10.1.1";
#BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.gecko.driver","C:/Users/Downloads/geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
driver = new FirefoxDriver(desiredCapabilities);
driver.get(url);
}
}
Even I tried other sample codes from different sites. Today after I upgrade all the softwares and run the code, it worked for me.

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.

How to use ChromeDriver in Selenium

I'm using windows on my system. I downloaded and extracted the chromedriver.exe file and I added it to my path.
Here is my code:
package com.chrometester.webdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class chromeTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
But it comes back with an error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable is a directory: C:\Users\Tgagn_000\Desktop\selenium\chrome
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome\\chromedriver.exe");
This should fix it. You should point to the driver file, not to its directory.
You are not adding the exe. Possibly
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
As error says, you have given directory path and not .exe path.
C:\Users\Tgagn_000\Desktop\selenium\chrome\ chromedriver.exe
Use below:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome\\chromedriver.exe");
You have to be careful about order of the code:
You have to write setProperty code first then initialize the ChromeDriver()
below code sequence will give you an error
WebDriver driver= new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
driver.get("https://www.google.com/");
Below code will work
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.google.com/");

Categories