I just got back into selenium testing and I'm using the basics:
initialized on top is
public static WebDriver driver=null;
and then depending on what is set in a properties file, it loads either the chromedriver or edgedriver.
System.setProperty("webdriver.edge.driver", System.getProperty("user.dir")+"//BrowserDrivers//msedgedriver.exe");
driver = new EdgeDriver();
and for chrome:
System.out.println(System.getProperty("user.dir")+"//BrowserDrivers//chromedriver.exe");
driver = new ChromeDriver();
it works for the most part, except it keeps opening two browser pages at once? which eventually leads to a disconnect because it uses the second page opened, then when it tries to close the page (ie, driver.quit()) to start the next test case, it successfully closes the page it ran the first test but because of the first page that was opened, it throws this error:
WARNING: Process refused to die after 10 seconds, and couldn't taskkill it
java.lang.RuntimeException: Couldn't detect pid
I am using the most updated and stable versions of both webdrivers and selenium4.5, and webdrivermanager5.3
Related
I am trying to run tests in parallel , however I am not able to. It triggers only one failing the other. I am using Chrome profiling option ("user-data-dir") to launch Chrome. If I disable this option, the tests run just fine in parallel. Can somebody tell me what I could be missing?
The argument that I have added is
public string ChromeProfilePath= "C:\Users\myuser\AppData\Local\Google\Chrome\User Data";
ChromeOptions options = new ChromeOptions();
options.AddArgument(string.Format("user-data-dir={0}", ChromeProfilePath));
ChromeDriver driver = new ChromeDriver(options);
It invokes the driver session with the current chrome profile successfully when a test is run but fails when run in parallel (invokes one instance, test it and fails the rest).
The error I receives for the failed one is
OpenQA.Selenium.WebDriverException : unknown error: failed to write prefs file
Note: I am using NUnit as test framework, C# and Selenium
I have the following setup:
A Selenium server hub running at "http://localhost:hubPortNum" (a service with the Jar file selenium-server-standalone-3.141.5.jar with parameter -role hub).
A Selenium Node at running "http://localhost:nodePortNum' (the service with Jar file with parameters: -Dwebdriver.chrome.driver=ChromeWebdriverPath -role node -port :nodePortNum).
I checked the URL for the hub and node instances to be sure they are working.
Whenever I try to create Remote Webdriver via Python script:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desiredCapabilities = DesiredCapabilities.CHROME.copy()
chromeOptionsRemote = webdriver.ChromeOptions()
chromeOptionsRemote.add_argument("--start-maximized")
chromeOptionsRemote.add_argument("--disable-session-crashed-bubble")
initRemoteDriver = webdriver.Remote(options=chromeOptionsRemote, command_executor='http://127.0.0.1:<nodePortNum>/wd/hub', desired_capabilities=desiredCapabilities)
print(initRemoteDriver.current_url)
The last line does print the current URL(which is "data:,"), that means Webdriver is created.
But the browser does not open on my local machine, that is it is running in the background and I don't know how to make it visible although it has worked in the past.
The troubleshooting steps I have made:
Reinstall latest selenium python package.
Re-Download latest Selenium server jar file.
Updating chrome.
adding chromeOptionsRemote.add_argument("--no-sandbox")
Making sure local Webdriver does open:
That is the line:
self.localDriver = webdriver.Chrome(options=chromeOptionsLocal,
desired_capabilities=desiredCapabilities)
does open the browser locally(the Chromedriver is in the path).
After I made these troubleshooting steps, I have tried the same configuration on a remote server and got the same result(browser not visible), so I think this is probably by design.
what configuration should I create for the browser to be visible?
Any help would be appreciated.
I was running the jar file by Always-Up: https://www.coretechnologies.com/products/AlwaysUp/
The problem was related to session 0 isolation: https://stackoverflow.com/a/26752251/2710840
in order to not run the application under session 0, I have enabled the Autologon feature:
defined the user under the application run as my user:
and executed the application from the context menu with the option to: "restart in this session"
In my script, I am opening private window of the browser to verify some contents in website. Below is the sample code:
if(osName.contains("Mac")){
new Actions(tester.getInternalDriver()).keyDown(Keys.COMMAND).keyDown(Keys.SHIFT).sendKeys("P").keyUp(Keys.COMMAND).keyUp(Keys.SHIFT).build().perform();
}
else if (osName.contains("Win") || osName.contains("nux")){
String pvtWin = Keys.chord(Keys.LEFT_CONTROL,Keys.SHIFT,"p");
tester.getInternalDriver().findElement(By.cssSelector("body")).sendKeys(pvtWin);
}
The code used to work fine when I was executing these test cases on my local (mac) machine or when Jenkins execute it locally on the Windows server.
But now the challenge I am facing when I am running my script on browserstack virtual machine. My local machine is Mac, and when I try to run something on the Windows(virtual) machine, it try to look for COMMAND button on the keyboard and test case fails. Is there any better way (javascript?) to open new private window which can work on any platform and for any browser?
I am looking for an option to open new tab, new window & new private window. I know javascript has limitations to override browser behavior.
You can execute the following JavaScript, to open a new window in your Selenium tests.
JavascriptExecutor js = null;
if (driver instanceof JavascriptExecutor)
js = (JavascriptExecutor)driver;
String scr = "window.open('https://www.google.com','_blank');";
js.executeScript(scr);
That said, any specific reason you wish to open a new window in your tests?
I am using a script created weeks ago in Firefox. When I loaded it up, Firefox opens but blanks out and doesn't run.
selenium = new DefaultSelenium("localhost",
4444,
"*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
"http://www.test.com/");
selenium.Start();
Do Firefox profiles get in the way? It opens up a new instance of Firefox everytime but doesn't go to the test site.
By writing "http://www.test.com/" for your selenium instance you just provide base url for your tests. To navigate to your test site you should use selenium.open("/") command. To navigate to "http://www.test.com/test.html" you should use selenium.open("/test.html") and so on.
I have a set of tests that I've developed using the Selenium IDE in Firefox. Tests run fine and all pass. Now I'm trying to port those over to C# and run the tests for IE using MSTest and selenium-server. Here's the sequence of events:
Run all tests on Firefox via Selenium IDE. All tests pass.
Export tests to MSTest (Nunit export, rename some stuff etc.)
Start selenium-server-1.0.3
Run the tests in VS2008. Selenium setup is: selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost/theSite/");; note that I can browse to http://localhost/theSite/ just fine.
4a. Confirm test is launching Firefox. Got result:
17:00:30.290 INFO - Preparing Firefox profile...
17:00:33.842 INFO - Launching Firefox...
First test ( waitForText "Login" in control "ctl00_TopNavBar_LoginStatus1") fails. Can't find the control.
if ("Login" == selenium.GetText("id=ctl00_TopNavBar_LoginStatus1")) break;
or
if ("Login" == selenium.GetText("ctl00_TopNavBar_LoginStatus1")) break;
All other tests fail as well -- same reason. Each test has a waitForText at the top.
Tried running the test in *chrome, *iexploreproxy and *firefox. All fail for the same reason.
Tried dumping a screenshot using selenium.CaptureEntirePageScreenshot("c:\\temp\\screenshots\\seleniumSite.png", "background=#FFFFFF"); and the screenshot is just a white bar.
So, my question is, how do I go about debugging this and figuring out what the real problem is? Clearly the page will render and the tests will pass. It's just when I try those tests in Selenium-RC that it fails so badly. Does the Selenium IDE have some bugs related to exporting tests to C#? (I have noticed one bug).
Are you executing selenium.Start() and selenium.Open()? Do you see the page opened in the browser?
Full initialization should be something similar to:
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost/");
selenium.Start();
selenium.Open("/theSite/");
Important Note! test your IDE tests by starting with an empty page. Also, when using the IDE, if a command only has one parameter put it in the Target field, even if it isn't a control name.