Jenkins + Selenium WebDriver + MSTest issue - selenium

I have created a test method in Visual Studio 2010 Ultimate that checks for the existance of two text boxes. I instantiate "InternetExplorerDriver" in the AssemblyInitialize() method and have setup Jenkins (on Windows 7) to run the MSTest method using the MSTestRunner plugin. The test seem to pass but I'm not seeing the DOS command window that I see when the "InternetExplorerDriver" instantiates and also I'm not seeing the Internet Explorer browser from loading the web page at all even though the test has passed. I also call the Quit() method on the webdriver in AssemblyCleanup. I'm running Jenkins service as my own NT account on my local box as running the service as "Local System Account" has issues. Here's my code snippet:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
webDriver = new InternetExplorerDriver(ieDriverDirectory, ieOptions);
The ieDriverDirectory has the InternetExplorerServer.exe running in 32bit mode. When I run the same test method from Visual Studio 2010 IDE or from the mstest.exe /testcontainer:, I'm able to see the DOS command window loading with the port number, the browser loading, the test method passing and the browser closing at the end when Quit() is called.

Related

Selenium navigate().to() stuck without error

I am running tests using TestNG, chromium and Selenium, in Java, on two machines:
my own laptop
a Mac Mini I do not have physical access to, that I connect via SSH, that has the same identical project structure as the project on my laptop.
Both machines operate behind the same corporate proxy.
The issue I am having is the following:
while the tests run smoothly on my machine, when I execute the command to start them on the Mac Mini, the execution stops when telling the Selenium WebDriver to navigate to any specified url using driver.navigate().to(url) or driver.get(url); causing the program to freeze without throwing any exception, essentially remaining on hold.
The code resembles the following:
in the #BeforeClass beforeClass() driver is initialized with an instance of ChromeDriver using ChromeOptions. Among other settings, I add the following arguments to the ChromeOptions like such chromeOptions.addArguments(options): where options is things like --disable-web-security, --allow-running-insecure-content, --allow-insecure-localhost and most importantly: --proxy-server=address:port (e.g. --proxy-server=172.26.44.146:3128).
in the #BeforeMethod beforeMethod() after checking that the driver is not null and that the URL url is a valid one, I execute something similar to the stylized following code:
try {
log.debug("Navigating to: " + url);
driver.navigate().to(url);
log.debug("Navigated to url: " + driver.getCurrentUrl());
} catch (Exception e) {
log.error("Unable to navigate at page: " + url.toString());
throw e;
}
As I said, when running the test on the Mac Mini via SSH using the command line in a similar fashion to: java -cp WORKSPACE org.testng.TestNG testng.xml the code stops executing and freezes at log.debug("Navigating to: " + url); essentially not producing other output and forcing me to manually stop the execution of the test.
As additional information, tests are run using chromium version 72.0.3609.0.
The ChromeOptions also receive the following proxy setting (like such: chromeOptions.setProxy(p);) where p is a Proxy with
p.setProxyType(ProxyType.MANUAL);
p.setAutodetect(false);
I suspect an issue with the proxy (and am doubtful I have correctly preconfigured the program), but, quite frankly, I am puzzled by the fact that the same script runs locally on my machine and is unable to run locally on the Mac Mini when the tests are launched via SSH.

Selenium Tests for Internet Explorer doesn't executs properly through Jenkins

We have our Health Check automated wherein all of our applications are logged onto, all via Selenium and only on Internet Explorer.
The code runs well when it was executed directly from a batch file. However, when Jenkins calls this batch (.bat) file, it doesn't execute it completely.
BTW the platform is Windows Server 2008 R2 Standard
This is the .bat file code
#echo off
set path="";
set path="E:\XXXX\jre1.8.0_141\bin";
pushd E:\Jenkins_Softwares\SeleniumCode\HealthCheck_jar
SET JAVA_OPTS=-Xmx4g -Xms512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=128m -XX:MaxHeapSize=512m
java -jar HealthCheck_JenkinsNG.jar
I've added these additional IE options in the JAVA code before launching the IE driver.
InternetExplorerOptions options = new InternetExplorerOptions();
options.introduceFlakinessByIgnoringSecurityDomains();
options.enableNativeEvents();
options.destructivelyEnsureCleanSession();
When Jenkins executes the batch file, the IE browser opens into the Login page. There's something odd when this page is displayed - the entire page alignment is disrupted and all elements get aligned to the left. (I'd like to stress that when the batch file is instead executed directly, there is no such page alignment disruption. The elements retain their original centre position. For some reason, Jenkins sets all of this to the left). The alignment is not exactly a deal breaker for me.
However, when username and password is entered via the Selenium code, it types into the perfect text boxes; but when the submit button is hit, the content in these textboxes turn blank and I'm unable to login. (When this same piece of code is executed via running the batch file directly, I'm able to login and The homepage of my application is displayed)
I doubt if there's something wrong with the selenium java code. Since, it executes properly, when run from the .bat file or even command line or even as a Java Application from an IDE.
For some reason, when this is executed from Jenkins it does not work.
Is there any options or settings that needs to be set when Jenkins works with Selenium on IE 11? Because I've tried tweaking the selenium code so much, they all yield the same result - The elements on the Login page get cleared after the submit button is clicked.
Also, just to mention, all of this is run on one Master node of Jenkins only. There are no slave nodes.
You need to take care of a couple of things as follows:
For the build process Jenkins would need the path of jdk. Simply jre may not suffice.
JDK 8u141 is ancient now and you need to upgrade to latest JDK 8u202
introduceFlakinessByIgnoringSecurityDomains() (in Java) and IntroduceInstabilityByIgnoringProtectedModeSettings() (in DotNet) is not an ideal solution to address the issues croping out of Protected Mode settings.
Here you can find a detailed discussion in Internet Explorer Protective mode setting and Zoom levels
To work with Selenium, InternetExplorerDriver and InternetExplorer you need to fulfill the Required Configuration

How to run Firefox in a Scheduled Task in Windows 2012 R2

Context: Microsoft Azure / Windows 2012 R2; C#; Selenium.Webdriver 3.4.0; Selenium.Support 3.4.0; Selenium.WebDriver.GeckoDriver.Win64 0.18.0; Firefox 54.0.1 (64-bit); geckodriver 0.18.0
I'm having enormous difficulty getting the Firefox session to launch inside a Windows Service Scheduled Task. I have no problem launching a Selenium-driven Firefox session from a C# exe interactively.
The Scheduled Task is set to "Run whether user is logged on or not", and "Run with highest privilege" is checked.
Part of the code in this boiled-down exe:
FirefoxProfile fp = new FirefoxProfile(#"C:\web\FirefoxProfiles\Thingo");
fp.AcceptUntrustedCertificates = true;
FirefoxOptions fo = new FirefoxOptions();
fo.Profile = fp;
fo.LogLevel = FirefoxDriverLogLevel.Debug;
fo.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
var ts = TimeSpan.FromMinutes(1);
IWebDriver driver = null;
try
{
driver = new FirefoxDriver(fo);
}
catch (Exception E)
{
Console.WriteLine(E.Message);
Environment.Exit(255);
}
After a minute of running that code I get the following Message:
The HTTP request to the remote WebDriver server for URL http://localhost:51289/session timed out after 60 seconds.
Interestingly, according to Task Manager, geckodriver.exe has --port 51289 on its command line.
I've been trying various combinations of FirefoxDriver invocations. None work as expected giving the above error or
Unable to find a matching set of capabilities
which makes even less sense. I've also tried a couple of different versions of Firefox, all to no avail.
Is there some Selenium, Firefox or, for that matter, Windows setting that might be affecting the running of the scheduled task? The task launches, and the binary runs, but Firefox is not launched.

Open new private window for cross browser testing using Selenium Webdriver

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?

can't find controls in page, but only when running via selenium-server

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.