Selenium don't recognize driver with jenkins - java

When I execute the jenkins job, the selenium test always fail with this error
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property
or when I set the driver path
not found /var/jenkins/.../C:/selenium/drivers/chromedrive
I have the follow environment
1 jenkins server and selenium hub running on linux
1 selenium node running on Windows.
The selenium node is running with the follow line
java -Dwebdriver.chrome.driver=C:/selenium/drivers/chromedriver.exe -jar selenium-server-standalone-2.53.1.jar -port 5556 -role node -hub http://192.168.15.99:4444/grid/register -browser "browserName=chrome, version=ANY, maxInstances=10, platform=WINDOWS"
Selenium hub and node can see each other.
Why I can't execute the tests? Look's like selenium are trying to execute on the hub, not on the node. How can I configure to do not ask for Chrome driver location?
My test
public class TesteSelenium{
private static final String APLICATION_CONTEXT = "/SYSA";
WebDriver driver;
HomePage home;
#Before
public void setUp() {
Properties p = PropertiesUtil.getProperties();
File file = new File(p.getProperty("webdriver.path"));
System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
driver = new ChromeDriver();
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
#After
public void finish() {
driver.close();
}
I use a properties file
host.address = http://jbossserver:8080
usuario.selenium.login = USER_SELENIUM
usuario.selenium.password = 123123
webdriver.path = C:/selenium/drivers/chromedriver
webdriver.type = webdriver.chrome.driver

You should have your parameters inside quotation marks in command line. Like this:
java -Dwebdriver.chrome.driver="C:/selenium/drivers/chromedriver.exe"

The code to run Selenium tests remotely is slightly different.
public void setUp() throws MalformedURLException {
Properties p = PropertiesUtil.getProperties();
//File file = new File(p.getProperty("webdriver.path"));
//System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
DesiredCapabilities capability = DesiredCapabilities.chrome();
//driver = new ChromeDriver();
WebDriver driver = new RemoteWebDriver(new java.net.URL("http://seleniumHubIp:4444/wd/hub"), capability);
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
My mistake was writing the code to run local tests on a remote selenium node.

Related

How to use selenium + phantomJs in spring boot?

I want to use selenium + phantomJs in my Spring boot project for scrapy. I can make it work in a normal java app like this :
System.setProperty("phantomjs.binary.path", path_to_phantomjs.exe);
PhantomJSDriver driver = new PhantomJSDriver();
driver.get(a_web_url);
WebElement e = driver.findElementByXPath("//button[#title='load-more']");
e.click();
JSWaiter.setDriver(driver);
JSWaiter.waitJsJQueryAngular();
String html = driver.getPageSource();
System.out.println(html);
driver.quit();
I set phantomJs in my spring boot project like this
main
|__java
|__resources
|__static
|__phantomJs
|__pahntomjs.exe
Whether I call :
public static void main(String[] args)
{
System.setProperty("phantomjs.binary.path", "static/phantomJs/phantomjs.exe");
SpringApplication.run(PricecompareApplication.class, args);
}
or :
#RequestMapping(value = {"/crawl"}, method = RequestMethod.POST)
public String crawlProduct(Model model, CrawlerOption crawlOption)
{
System.setProperty("phantomjs.binary.path", "static/phantomJs/phantomjs.exe");
PhantomJSDriver driver = new PhantomJSDriver(); // <-- always error at this
}
My program alway throw this error when i create a new PhantomJSDriver :
java.lang.IllegalAccessError: tried to access class org.openqa.selenium.os.ExecutableFinder from class org.openqa.selenium.phantomjs.PhantomJSDriverService
It seem like I set the wrong path or I can not access the resources, but when I use the path to phantomjs.exe in my console app (the first code block in my question), still the same error .
How can I config selenium + phantomjs in Spring boot?

The driver executable does not exist: D:\Firefox\geckodriver.exe

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class sasas {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
this is the sample code i am trying. when i run i get the error message as "The driver executable does not exist: D:\Firefox\geckodriver.exe" please help me to proceed. i added the location in environmental variable then too i get this error . PATH i added as D:\Samplecode.
kindly help me
(1) To use gecko driver, make sure you are using Firefox version 55 and above to get better gecko Web-Driver feature support, find out more here
(2) Perhaps, you should downgrade Selenium to a lower version i.e. version 2.53.1. Selenium version 2.53.1 runs perfectly on Firefox 47.0.1 and lower, does not require using web driver API. I have ran your code against this and it worked fine.
public class Sasas {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
Use the relative path:
JAVA
1.- In your project, create the Drivers/Win/Firefox/geckodriver.exe folder and add your .exe
2.- Replace:
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
For:
String path = System.getProperty("user.dir") + "/Drivers/Win/Firefox/Geckodriver.exe";
System.setProperty("webdriver.gecko.driver", path);
Note: using the relative path is the most optimal

Running Selenium Webdriver script in Chrome

I am trying to run Selenium Webdriver script in Chrome, have added following lines in my existing script
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\chromedriver.exe");
private WebDriver driver = new ChromeDriver();
I am building my script in Intellij using Java. Not sure why i am getting "can not resolve symbol setProperty". I tried changing JRE and JDK files but nothing has worked. Any help would be appreciated.
Adding code
public class StartCaseJava extends TestCase {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
// Getting Date and Timestamp for Last Name
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyHHmmss");
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
// private WebDriver driver = new ChromeDriver();
// driver = new FirefoxDriver();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private WebDriver driver = new ChromeDriver();
public void testStartCaseJava() throws Exception {
// System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
It's a long shot but should this
C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
maybe be renamed to C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
or are the double back slashes actually a necessary feature?
This might be because of one missing '\' before chromedriver.exe in your setProperty string.
Try using :
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
I am able to run my script using Chrome now. Here is the small sample declaration I used:
class StartCaseJAva {
static WebDriver driver;
public void testcasejava()) {
System.setProperty(path);
driver = new ChromeDriver();
}
}

Selenium Automation a new window handle

So after my code runs a new window shows up to download a CSV. A window pops up to either save the file or to open it with excel. How would I change windows to download that csv to a path using selenium.
public class automation {
public static void main(String[] args) {
FirefoxProfile profile = new FirefoxProfile();
String path = "C:\\Users\\K344975\\Desktop";
profile.setPreference("browser.download.dir",path);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/xls;text/csv");
profile.setPreference("plugin.disable_full_page_plugin_for_types",
"application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;application/xls;");
WebDriver driver = new FirefoxDriver(profile);
driver.get("");
String winHandle = driver.getWindowHandle();
System.out.println(winHandle);
//driver.quit();
}
}
I also tried making a profile
Well you have firstly patch profile and only after it create browser, and for plugin disable you could use
profile.setPreference("plugin.disable_full_page_plugin_for_types",
"application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;")

Configuring PhantomjsDriver to run selenium test cases

Am new to this PhantomjsDriver in selenium webdriver.I need to run my selenium scripts in server without GUI. Please can anybody tell me how to achieve this. I need a head's up from the start of how to configure Phantomjs Driver,usage in server and rest.Below is my selenium code which i run through GUI , now i have to run these cases in server without GUI. What modifications i have to do so hat i can achieve the above task.
public static void main(String[] args) throws IOException{
login =args[0];
user = args[1];
pwd = args[2];
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory(args[3]);
testng.setTestClasses(new Class[] {
CreateMultiRecordTest.class, UpdateMultiRecordTest.class,
DeleteMultiRecordTest.class
});
testng.addListener(tla);
testng.run();
Finally after a weeks time i found a solution to configure PhantomJs for my framework.Here's the solution.
DesiredCapabilities cap = new DesiredCapabilities();
java.io.File f = new java.io.File("");
String path = f.getAbsolutePath()+"\\ghostdriver-master\\src\\main.js";
cap.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY,path);
driver = new PhantomJSDriver(cap);
This worked for me:
DesiredCapabilities dCaps = new DesiredCapabilities();
dCaps.setJavascriptEnabled(true);
dCaps.setCapability("takesScreenshot", false);
dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.7-windows\\phantomjs.exe");
PhantomJSDriver driver = new PhantomJSDriver(dCaps);
...

Categories