How do I get started with Selenium? - java

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

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;

Error occurred when use gecko in selenium

i am new to selenium automation and i have follwed the instruction of the web site and i have use gecko driver but it shows message as below.i have follow few web articals but i didnt get the solution.i am using firefox 60.0.2 (64-bit) , Selenium 3.12 and gecko driver 20.1 .
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. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
here is my code
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class userLogin{
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver ","E:\\\\Selenium\\\\geckodriver-v0.21.0-win64\\\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
}
Edit: reformat code
Replace it
System.setProperty("webdriver.gecko.driver","E:\\\\Selenium\\\\geckodriver-v0.21.0-win64\\\\geckodriver.exe");
it has extra space...

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.

Selenium in java eclipse linux

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.

Trying to test a web application using selenium webdriver, a error that pops showing not abe to connect to the local host

i am running the project file on the same system on which i am testing
i am using selenium web driver and eclipse for testing the project but error is shown, when i run the selenium script for testing login page.
**error** :
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
l,"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 (x86)\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi","installDate":1462864767739,"updateDate":1462864767739,"applyBackgroundUpdates":1,"skinnable":true,"size":22012,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"
{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"46.0.1","maxVersion":"46.0.1"}],"targetPlatforms":[],"seen":true}
so after this i had set the proxy
and tried but the result was same
but when i tried to copy the link and manually pasted it in Mozilla and searched it was successfully showing the login page
CODE SELENIUM
Package TESTING
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class demo {
`public static void main(String[] args) {
WebDriver driver= new Firefox Driver();
driver.get("localhost:8080/Chaitanya");
driver.findElement(By.name("username")).send Keys("login");
driver.findElement(By.name("login.password")).send Keys("password");
`
I had same issue in my recent project (FYI: issue is caused by port) , fixes for this is:
Try to update latest version of selenium, this should fix it. if not, dirty work around for this is
WebDriver driver;
try {
driver = new FirefoxDriver();
}
catch(Exception e) {
driver = new FirefoxDriver();
}
There few typo error....
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class demo {
public static void main(String[] args) {
WebDriver driver= new FirefoxDriver();
driver.get("http://localhost:8080/Chaitanya");
driver.findElement(By.name("username")).sendKeys("login");
driver.findElement(By.name("login.password")).sendKeys("password");
}
if Still same error occurs.
Please update Selenium Jar to latest( http://www.seleniumhq.org/download/ ), and update Firefox browser to latest version.

Categories

Resources