Hi can somebody tell me please what am I doing wrong in running parallel tests on Selenium server?
I have this simple parallel tests in the class:
package tests;
import categories.Category1;
import com.google.code.tempusfugit.concurrency.ConcurrentTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
#RunWith(ConcurrentTestRunner.class)
#Category(Category1.class)
public class ParalelTest extends Base
{
protected String siteUrl = "/waitforit.php";
#Before
public void setUp()
{
driver.get(baseUrl + siteUrl);
}
#Test
public void test1()
{
System.out.println("test1() thread name: " + Thread.currentThread().getName());
}
#Test
public void test2()
{
System.out.println("test2() thread name: " + Thread.currentThread().getName());
}
#Test
public void test3()
{
System.out.println("test3() thread name: " + Thread.currentThread().getName());
}
#Test
public void test4()
{
System.out.println("test4() thread name: " + Thread.currentThread().getName());
}
}
Here is the screen with error:
What is wrong with that? Without server it works fine.
This error message...
org.openqa.selenium.NoSuchSessionException: no such session
...implies that the chromedriver=80.0 wasn't able to initiate/spawn a new Browsing Context with Chrome Browser v80.0.
Your main issue is the version incompatibility between the binaries you are using as follows :
Though you are using latest Selenium Client version 3.141.59, Chrome Driver version 80.0 and Chrome version v80.0.
Your JDK version is 1.8.0_65 which is pretty ancient.
Solution
A generic solution would be to:
Upgrade JDK to recent level of JDK 8u241.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test.
Update
However, the invalid session ID error is a WebDriver error that occurs when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid.
A WebDriver session can be deleted through either of the following ways:
Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the quit() method.
Implicit session deletion: A WebDriver session is implicitly deleted when you close the last window or tab invoking close() method.
Reference
You can find a relevant detailed discussion in:
selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python
Related
Selenium sometimes fails to close after calling driver.quit() when it gets stuck at "connection to server was reset" or when it simply "stops responding". When this occurs, it is impossible kill the process (browser) using WebDriver directly, the only way I can think of is by retrieving the PID of browser and destroying process via:
String cmd = "taskkill /F /PID " + pidOfBrowser;
Runtime.getRuntime().exec(cmd);
I'm aware of this response which suggests retrieving a list of processes currently running and filtering it down to Firefox browser. However, as someone pointed out in one of the comments this does not work if a user has many concurrent sessions running and only wishes to kill a select few.
Another solution suggested in the comment section of that thread is to get a list of PIDs from the browser before starting, and only close those that were not running before the test started (so any browser launched manually before tests began won't close)
However, this does not apply to my situation because I am launching many browsers at a time from my program (not manually) and only wish to close some of the browsers launched (the ones that are hanging and not responding to WebDriver anymore).
How can I get PID of a specific Firefox WebDriver session (ideally when it is created) so I can kill the process later if it hangs or gets 'stuck'?
Thanks!
You can kill the Browser instance initiated using Selenium retriving the PID from the capabilities object and then invoking getRuntime() with taskkill /PID as follows:
Code Block:
import java.io.IOException;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Kill_Firefox_PID {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
System.out.println("moz:processID value is : "+cap.getCapability("moz:processID"));
Runtime.getRuntime().exec("taskkill /PID "+cap.getCapability("moz:processID"));
}
}
Console Output:
moz:processID value is : 8492
In Java 9+ version you can do like this. It is taken from here
public static void main(String[] args) {
ProcessHandle.allProcesses()
.forEach(process -> System.out.println(processDetails(process)));
}
private static String processDetails(ProcessHandle process) {
return String.format("%8d %8s %10s %26s %-40s",
process.pid(),
text(process.parent().map(ProcessHandle::pid)),
text(process.info().user()),
text(process.info().startInstant()),
text(process.info().commandLine()));
}
private static String text(Optional<?> optional) {
return optional.map(Object::toString).orElse("-");
}
and then get info of the process like above. There is a totalCpuDuration() method in ProcessHandle.Info class. use this to see which process is taking long and then act accordingly. There are other methods in that class that can be beneficial in your scenario. Hope this helps.
I'm using chromedriver and Java to run automatic web tests. To be able to use extensions in Chrome I'm using my existing browser profile. Since then I'm experiencing following:
run a selenium test with green result
open Chrome manualy (or run some test)
get error message "chrome application did not close properly"
I'm closing browser and the driver with this:
#AfterClass public static void tearDownClass() {driver.quit();}
I've tried:
#AfterClass public static void tearDownClass() {driver.close();}
but this closes just browser, not the driver.
After some attempts to fix I simulate pressing CTRL+SHIFT+Q:
package SSO_CWP_APPROVAL;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
public class Keyboard_events {
public static WebDriver driver;
public Keyboard_events(WebDriver driver) {Keyboard_events.driver = driver;}
public void ctrl_shift_q() throws AWTException, InterruptedException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_Q);
rob.keyRelease(KeyEvent.VK_CONTROL);
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyRelease(KeyEvent.VK_Q);
Thread.sleep(1000);
}
}
The sleep is in this case neccessary. Sleeping less time and get the error again.
#AfterClass public static void tearDownClass() throws AWTException, InterruptedException {K_events.ctrl_shift_q();driver.quit();}
binaries:
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200
chromedriver.exe 2.42
JDK 8u151
Chrome 69.0.3497.100
Is there any better way how to close browser and the driver?
It was propably bug in Google Chrome 69. In version 70 and 71 works driver.close();driver.quit(); just fine.
Mac book air
Java 1.8
Appium Desktop version 1.8.1
Hi Guys,
I am building a TestNG framework and and I want to start my appium Desktop server programmatically for my tests. So what I decided to do was to create a java test class to invoke appium for me, I thought I'd coded it ok but when I ran it just to check. Got a 'null point exception'
1. What did I do wrong?
2. How do I fix this?
Here is the launch appium programmatically code:
package aappiumLaunchServer;
import java.io.File;
import io.appium.java_client.service.local.AppiumServiceBuilder;
public class LaunchAppium {
private static AppiumServiceBuilder service;
public static void main(String[] args) {
//text
stopAppium();
startAppium();
stopAppium();
}
public static void startAppium() {
System.out.println("Start Appium Server");
service = new AppiumServiceBuilder().usingDriverExecutable(new File("/usr/local/bin/node"))
.withAppiumJS(new File ("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js "));
service.build().start();
System.out.println("You can now use Appium Server");
}
public static void stopAppium(){
System.out.println("Appium Server is shutting down....");
try {
service.build().stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Your current appium session is terminated... ");
}
System.out.println(" Appium Server has shut down. Thank you for using Appium Server");
}
}
Here is the message from eclipse
appium Server is shutting down....
java.lang.NullPointerExceptionYour current appium session is terminated...
Appium Server has shut down. Thank you for using Appium Server
Start Appium Server
at aappiumLaunchServer.LaunchAppium.stopAppium(LaunchAppium.java:44)
at aappiumLaunchServer.LaunchAppium.main(LaunchAppium.java:16)
Exception in thread "main" io.appium.java_client.service.local.InvalidServerInstanceException: Invalid server instance exception has occured: The invalid appium node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js has been defined
at io.appium.java_client.service.local.AppiumServiceBuilder.validateNodeStructure(AppiumServiceBuilder.java:102)
at io.appium.java_client.service.local.AppiumServiceBuilder.checkAppiumJS(AppiumServiceBuilder.java:294)
at io.appium.java_client.service.local.AppiumServiceBuilder.createArgs(AppiumServiceBuilder.java:389)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:342)
at aappiumLaunchServer.LaunchAppium.startAppium(LaunchAppium.java:29)
at aappiumLaunchServer.LaunchAppium.main(LaunchAppium.java:17)
Caused by: java.io.IOException: The node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js doesn't exist
at io.appium.java_client.service.local.AppiumServiceBuilder.validateNodeStructure(AppiumServiceBuilder.java:104)
... 5 more
Just with the debug mode at startAppium method try to create a file instance
new File ("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js "
This will probably show you that it can not find the file
It seems you are trying to use JS main.js file from Appium.app instead install appium using node and you should find your file under
Appium_MAIN_JS = System.getenv(APPIUM_HOME)+"/node_modules/appium/build/lib/main.js" (define APPIUM_HOME in your bash_profile)
It's possible that your code does not have access to reach Appium.app (I presume it is in Application folder)
and then you can call
.buildService(new AppiumServiceBuilder().withAppiumJS(new File(Appium_MAIN_JS)))
If you installed NodeJS from install for Windows and \AppData\Roaming\npm is empty, you should open Windows PowerShell and run command: npm install -g appium
I've been struggling with this for the past few hours. I'm trying to install Selenium web driver and have been running into a bunch of errors which prevent me from running the test page. I'm pretty sure my most recent issue is with this code:
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:/Users/theone/Downloads/geckodriver 2.exe");
Would really appreciate any feedback on second steps!
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTestCase {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:/Users/theone/Downloads/geckodriver 2.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
driver.quit();
}
}
You can setup Selenium with GeckoDriver either with webdriver.gecko.driver property or using environment properties. It would be good if you the latest version of Firefox, GeckoDriver and Selenium 3.0
Check out this article which provides setup using both these ways -
http://automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/
C:/Users/theone/Downloads/geckodriver 2.exe
There's a space in the path, it may work if you rename your file geckodriver2.exe.
The classic google test here from selenium website, it works in FF on vista. On IE7, apparently doesn't find the window object. Selnm gets farther in the test (On IE) when I change config to using "*iexploreproxy", (instead of "*iexplore") but I cannot use that because it causes untrusted security certificate warnings. I installed selenium RC 1.0.1, and checked it is running on my box, I am not using any other tools such as bromine. I am running on Eclipse.
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com/", "iexplore");
// We instantiate and start the browser
}
public void testNew() throws Exception {
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
if(! selenium.isTextPresent("Results * for selenium rc"))
throw new Exception("failed");
}
}
I found this error to occur when IE was running in protected mode. You can disable protected mode by going to IE Tools->Internet Options->Security and click the check box.
I found that the internet options, connections, LAN settings has a automatic configuration script/custom profile that was interfering somehow in IE. It works now!
Here was the path just for history.
file://C:/Users/myname/AppData/Local/Temp/customProfileDir4b9b53c99d684ec4952cf8a721790c85/proxy.pac