Selenium in java eclipse linux - java

I have the following error
and here is my code
import org.openqa.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class io {
/**
* #param args
*/
public static void main(String[] args) {
WebDriver driver=new ChromeDriver();
}
}
I mention that this error appear only when a do "new ChromeDriver()" .If i let without that it don't do that and i don't know what i need to do to solve it.Please help.
EDIT: After i dowloaded the chrome driver when i execute the program it works fine but when i am debugging it suspend.Why this happens?

You have to set the System property before creating an instance of the chrome driver. Download chromedriver in your machine and add below line to your code before initializing the chromedriver.
System.setProperty("webdriver.chrome.driver","<path to chromedriver>");
Hope it helps.

Take a look at my project, https://github.com/codezombies/easytest. One of the methods on my project makes chrome driver initialization happens before the tests run ( actually upon driver type selection ). With that said, i embedded the 3 types of chrome driver on different OS, and it should work as well on linux.

Related

selenium configuration ChromeDriver works but ChromeOptions not found

I have just configured Selenium for Java on Eclipse on a new PC (have configured without issue for a number of times in the past) and can run a sample code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AutoLogin {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://pathtodrive//chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Open Register
driver.get("https://google.com");
// Maximize browser
driver.manage().window().maximize();
}
}
The above code works perfectly and could see google.com open in the browser window. Now I wanted to change some of the default options like default download directory, certificate check etc. I could see the ChromeOptions.class under imported client-combined-3.141.59.jar
But when I write
import org.openqa.selenium.chrome.ChromeOptions;
the eclipse shows error
the import org.openqa.selenium.chrome.ChromeOptions cannot be resolved
please advise what could be wrong? and which configuration I am missing
os: windows 10 64 bit eclipse: Helios Service Release 1
(Build id: 20100917-0705 ) 32 bit java: jre1.8.0_261 32 bit
In addition to the specified import, the driver must be properly initialized. In the code below, it works. That is, first we specify the options, then we use this variable to create a web driver instance.
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito", "headless", "disable-gpu", "window-size=1366,768", "ignore-certificate-errors");
newWebDriver = new ChromeDriver(options);
And of course the corresponding import.
import org.openqa.selenium.chrome.ChromeOptions;

Run as java application brings up select java application screen, no further instruction

Asking because I'm following Guru99's time sensitive selenium course and the code that I have downloaded as part of my project will not run as a java application.
It is supposed to be ran with only this code:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestScript01 {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.demo.guru99.com/V4/";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// Enter username
driver.findElement(By.name("uid")).sendKeys("xxxx");
// Enter Password
driver.findElement(By.name("password")).sendKeys("xx");
// Click Login
driver.findElement(By.name("btnLogin")).click();
}
}
However, I have added: import org.openqa.selenium.WebDriver; and System.setProperty("webdriver.chrome.driver",
"C://selenium/chromedriver.exe");
I've also not included my real username and password in code above
I have downloaded Chrome driver to the selenium folder in my C drive
I was trying to run from firefox initially but was stuck on Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms errors, which I downgraded my firefox for as that worked for many on this site, but it was still giving me the same error so I switched to Chrome, which selenium seems to prefer. I'm using the latest version of Chrome and Firefox 47.0
I'm using selenium 3.6.0 and jdk 1.8.0_111
When trying to run as an application, according to the instructions, I seem to be in a loop where I keep getting this screen:
I have never had to select an option in order to run a selenium script before, not sure why I'm getting it now or what I'm supposed to select if any.
I have googled but it seems that most instructions for running selenium tests do not include this pop-up. I thought that instantiating a new WebDriver object and selecting the right imports were enough, what am I missing?
You havn't mentioned the Selenium, ChromeDriver, Chrome Browser and JDK versions. Assuming you are using the latest version of Selenium, ChromeDriver, Chrome Browser and JDK, I would suggest a few steps as follows:
Instead of import org.openqa.selenium.*; always use import org.openqa.selenium.WebDriver; and the required ones.
While working with Selenium 3.x (Java) it is mandatory to mention the following line :
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
In this line you have to either use single front slashes / or you have to use escaped back slashes \\
The screen with Select Java Application indicates there are multiple overlapping imports in your project or methods from overlapping jars. We need to keep only the used imports in your script & used jars in your project and remove the other imports/jars from your script/project to keep it simple.
From your IDE take a Project -> Clean for all the projects and keep Build Automatically selected.
Error Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms can arise for many reasons. The best remedy is to uninstall the Browser with Revo Uninstaller, Run CCleaner to wipe out all the rotten OS stuffs and take a system reboot and trigger your Test.

How do I get started with Selenium?

I'm trying to begin using Selenium. I have downloaded the Selenium Stand Alone Server, Selenium for JAVA, and GeckoDriver. I added all of the .jar files from the Stand Alone Server and Selenium for JAVA to my buildpath in Eclipse. When I run my program, I get the follwing error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
FirefoxOptions cannot be resolved to a type
FirefoxOptions cannot be resolved to a type
at check.Selenium_Basic.main(Selenium_Basic.java:14)
My code is below:
package check;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium_Basic {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\User1\\Documents\\geckodriver-v0.16.1-win32\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //This is the location where you have installed Firefox on your machine
FirefoxDriver driver = new FirefoxDriver(options);
driver.get("http://www.google.com");
}
}
Do you guys know why this doesn't work? Is there a good step by step guide that will allow me to start using this? I have looked at many guides but I can't figure out what I've done incorrectly.
The code looks good. You are missing an import statement for FirefoxOptions. You can add the below line to your code with other import statements & your code should work.
import org.openqa.selenium.firefox.FirefoxOptions;
Also, you don't need to use Selenium StandAlone Server. Selenium Java alone with GeckoDriver would work fine. Make sure that you are using Selenium 3.4, as its compatible with Gecko 16.
You can also check this link for complete setup related steps -
Selenium 3.4 – Complete Guide to the latest Selenium WebDriver

Selenium FireFoxDriver unable to connect

I've been attempting to use Selenium to drive Firefox for the fist time. I used virtually identical code to drive Chrome without issue. However, when I attempt to use the Firefox driver, the browser opens, stalls, and then, after about 60 seconds, I get an error report that reads as follows:
Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
4474-a285-3208198ce6fd}","syncGUID":"dcskEFBTLyBH","location":"app-global","version":"48.0.1","type":"theme","internalName":"classic/1.0","updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{"32":"icon.png","48":"icon.png"},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Default","description":"The default theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla Contributors"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"C:\\Program Files\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi","installDate":1471881400240,"updateDate":1471881400240,"applyBackgroundUpdates":1,"skinnable":true,"size":21905,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"48.0.1","maxVersion":"48.0.1"}],"targetPlatforms":[],"seen":true}
1472056603181 addons.xpi DEBUG getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd}
I've checked other guides and all they recommend is that I update my .jar files. I am using selenium-java-3.0.0-beta2 and Firefox 48.0.1 to test so my files are up to date. I would like to get this to run properly.
UPDATE: code still does not work and I've set the System property to set the geckodriver properly. However, I still cannot get the driver to function properly. It won't even launch the browser any more.
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SimpleFireFoxDriver {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.youtube.com");
System.out.println("Made it to the promised land");
driver.quit();
}
}
Edit: Also the path to FireFox itself is located here:
"C:\Program Files\Mozilla Firefox\firefox.exe"
Changing the system property worked for me. Change it to following:
System.setProperty("webdriver.firefox.marionette","src\\test\\java\\lib\\geckodriver.exe");
driver= new FirefoxDriver();
Hope that helps.
This is happening because you have set the wrong system property. You need to set system property as follows:
System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver.exe");
Selenium firefox driver expects this System property to be set before initiating marionette driver and launching firefox. And if you don't set any System property and try to instantiate Firefox driver then you will get following error :
"The path to the driver executable must be set by the webdriver.gecko.driver system property."
Hope this helps.
Changing "webdriver.gecko.driver" with "webdriver.firefox.marionette" saved my life!
example:
Correct
System.setProperty("webdriver.firefox.marionette","C://selenium/gecko/geckodriver.exe");
Not correct
System.setProperty("webdriver.gecko.driver","C://selenium/gecko/geckodriver.exe");
Download the latest Gecko driver V0.17.0 and this resolved my error without changing the setProperty or downgrading of Firefox browser.
Not so sure, whether this may help you.
Changing "webdriver.gecko.driver" with "webdriver.firefox.marionette" saved my Life Also Downgrading Firefox from 50 to 36
Or try this code:
System.setProperty("webdriver.firefox.marionette", "D://Driver//geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Selendroid not starting virtual device

I am trying to get started with selendroid and selenium for android virtual devices but im having a few issues. I have all the java/android sdk/eclipse software installed and i can run selenium chromedriver tests fine. BUT when i try to run a test that starts the android virtual device, it never starts and the app doesnt list in the http://localhost:4444/wd/hub/status page.
My generic test is below. If i run this code below, the server will start and i can see my local host data from the status page BUT my app version doesnt get listed. If i run java -jar selendroid-standalone-0.11.0-with-dependencies.jar -app selendroid-test-app-0.11.0.apk then i can see my app listed in the status page. I have tried with the app pre-loaded on the virtual device and also wit the signed app on the virtual device, neither have worked.
I am pretty much at a dead end as where else to look for the solution. I have spent 3-4 days looking for a solution but cant seem to find it. Currently in the java project i have only the selenium and selendroid .jar file dependencies loaded. I have yet to install any jUnit or anything 'test' releated
package testpackage;
import io.selendroid.SelendroidCapabilities;
import io.selendroid.device.DeviceTargetPlatform;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class testClass {
/**
* #param args
*/
public static void runSelendroidTest() throws Exception {
// specify test capabilities (your 'test environment')
SelendroidCapabilities capa = new SelendroidCapabilities("io.selendroid.testapp:0.11.0");
// explicitly state that we want to run our test on an Android API level 17 device
capa.setPlatformVersion(DeviceTargetPlatform.ANDROID17);
// explicitly state that we use an emulator (an AVD) for test execution rather than a physical device
capa.setEmulator(true);
// start a new WebDriver
WebDriver driver = new SelendroidDriver(capa);
// execute a very simple test
WebElement inputField = driver.findElement(By.id("my_text_field"));
Assert.assertEquals("true", inputField.getAttribute("enabled"));
inputField.sendKeys("Selendroid");
Assert.assertEquals("Selendroid", inputField.getText());
// quit testing
driver.quit();
}
}
maybe you wanna make sure if the 'DeviceTargetPlatform.ANDROID17' really targets your device as ANDROID17. That is when you run your test, check the log. If the http://localhost:4444/wd/hub/status works for you, kindly check your device target there.
I had the same issue before and i just figured out that I was using the 'ANDROID18' not ANDROID17.

Categories

Resources