PhantomJS in TFS - c#

We are trying to run Selenium Tests as part of our release with TFS Release Management with PhantomJS (Headless browser).
The tests runs smooth locally (of course =)) but when I try to run them on VSTF the test cant find any element.
I think that something is blocking the driver from starting, If I run the test locally with PhantomJs i get the Windows Security Alert.
Could it be that this is blocking in VSTF?
and in that case, is it possible to send an option when setting browserType?
switch (settings.BrowserType)
{
case BrowserType.InternetExplorer:
driver = new InternetExplorerDriver("Missing path!!");
break;
case BrowserType.FireFox:
driver = new FirefoxDriver();
break;
case BrowserType.Chrome:
driver = new ChromeDriver(settings.ChromeDriverPath);
break;
case BrowserType.PhantomJS:
driver = new PhantomJSDriver(settings.PhantomDriverPath);
break;
}

You could temporarily turn off firewall for troubleshooting to see if the test could be run successfully on the server.
And there has been a wonderful blog shows how to running Selenium Tests as part of your release with VSTS Release Management. You could double check your build and release definition settings to narrow down the issue.

Related

Unable to Pass Selenium tests within Azure Pipeline

I am trying to build out UI test cases in our CI/CD build pipeline within Azure Devops and I can't seem to workaround a basic use case where we route to one of our internal web pages landing screen, and verify a Div with a Selector exists.
[Theory]
[InlineData("Chrome")]
public void TestFindElementByID(string browser)
{
var driver = SetupDriver(browser);
try
{
driver.Navigate().GoToUrl(TestConfig.WebURL);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("mainRepDiv")));
//Thread.Sleep(2500);
//IWebElement element = driver.FindElement(By.Id("mainRepDiv"));
Assert.True(element != null);
}
catch (Exception ex)
{
}
finally
{
driver.Quit();
}
}
In my web page, I have a basic div that I just want to see that it exists once the chromedriver navigates to the URL.
Element To Check
And here is a log of the Azure Pipeline failing on this test.
Pipeline Log
I do have an additional test that just asserts that the Browser is able to go to the URL, and the URL is in fact what I routed to, and that test passes.
driver.Navigate().GoToUrl(TestConfig.URL);
Assert.True(driver != null);
Assert.True(driver.Url.ToLower() == TestConfig.URL.ToLower());
There also some layers that likely are making this more difficult such as
The Azure Pipeline is running off a Self-hosted agent.
This self hosted agent is also the same server that is running the application were trying to test against.
There is OS level Security that shows up if you route to it normally, however as the Windows Server that is running the application would have a valid credential to the browser, it should bypass this dialog.
So things I have tried so far:
Various ways of actually selecting the Element incase that was off, such as By ID that is shown, or by XPath as well.
Using the Thread.Sleep() or the WebDriver Wait commands to determine if it was a page load.
Implementing AutoIT Process that will run in the event there is in fact an OS popup that displays, so I can prefill the Admin Username/Password credentials incase it actually is not getting there.
Running the unit test locally on my client PC, publishing the code to the Server, running the code from the application server directly and verifying it works there as well.
So I'm a bit at a loss what to check on regarding the Azure Pipeline build itself and why the ChromeDriver is unable to pass this test.
Finally - if this is also helpful, here is a screenshot of the YML file pertaining to the Tasks.
- task: VSTest#2
displayName: "Run UI Tests"
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\Project.Test.UI.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
uiTests: true
Unable to Pass Selenium tests within Azure Pipeline
Please make sure your private agent run as an interactive process.
According to the document Interactive vs. service:
You can run your self-hosted agent as either a service or an
interactive process. After you've configured the agent, we recommend
you first try it in interactive mode to make sure it works. Then, for
production use, we recommend you run the agent in one of the following
modes so that it reliably remains in a running state.
As an interactive process with auto-logon enabled. In some cases, you might need to run the agent interactively for production use -
such as to run UI tests. When the agent is configured to run in this
mode, the screen saver is also disabled. Some domain policies may
prevent you from enabling auto-logon or disabling the screen saver. In
such cases, you may need to seek an exemption from the domain policy,
or run the agent on a workgroup computer where the domain policies do
not apply.
So, if your agent not run as interactive mode, please configure a another agent in interactive mode to test this issue.

Is there a way to activate IE mode in Edge Options?

Hello,
I want to achieve this by having an option inside the EdgeDriver but I cant seem to find it anywhere on the map?
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver.
Is there a way to achieve this great thing? [pun intented]
I can see 2 questions in this thread.
Is there a way to activate IE mode in Edge Options?
There is no way to activate IE mode bypassing the Edge options parameter in the Selenium Edge driver.
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver. Is there a way to achieve this great thing?
Yes, it is possible to automate the IE mode in the new MS Edge browser using the Selenium web driver.
The new Microsoft Edge allows you to run IE11 validation for legacy sites in addition to your modern experiences. To run your IE11 tests in Microsoft Edge, download the IEDriverServer from Selenium. Then you must pass in a capability to put Microsoft Edge into IE Mode and then run your tests.
Because this capability puts the whole browser into IE11 Mode, you cannot simultaneously test content that should render in the modern Chromium engine, but you should be able to run all of your IE11 tests and validate the rendering in Microsoft Edge. Note that this code requires an update to IEDriverServer which should be included in the next release of Selenium.
After you download the new IEDriverServer from SeleniumHQ and follow the directions for the “Required Configuration” as documented here, you can run the following code to launch the new Microsoft Edge in IE11 mode and run some tests:
static void Main(string[] args)
{
var dir = "{FULL_PATH_TO_IEDRIVERSERVER}";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieService = InternetExplorerDriverService.CreateDefaultService(dir, driver);
var ieOptions = new InternetExplorerOptions{};
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", #"\\msedge.exe");
var webdriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
webdriver.Url = "http://www.example.com";
}
Output:
Notes:
Make sure you are using the latest version of the IE driver server.
I suggest making a test with the latest version of the Stable Edge browser.
Try to pass the full path of the Edge browser in the 'ie.edgepath' capability. For example:
ieOptions.AddAdditionalCapability("ie.edgepath", #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");
Make sure you close all the already opened instances and tabs of the Edge browser before running the code. Otherwise, it will generate an error.
References:
Scroll to the Automating Internet Explorer mode point in this link.
kypflug/webdriver-edge-ie-mode.cs
Below code (which is in VB.NET, but you can easily modify it to C#) will start Chromium Edge in IE Mode
Dim ieService = InternetExplorerDriverService.CreateDefaultService("DIRECTORY_PATH_HAVING_IEDriverServer.exe", "IEDriverServer.exe")
Dim ieOptions = New InternetExplorerOptions
ieOptions.IgnoreZoomLevel = True
ieOptions.AddAdditionalCapability("ie.edgechromium", True)
ieOptions.AddAdditionalCapability("ie.edgepath", "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
Dim driver = New InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(60))
driver.Navigate().GoToUrl("https://example.com")
You can download IEDriverServer from https://www.selenium.dev/downloads/

Selenium tests open IE in background on TFS build server

we are trying to use Selenium for testing our MVC application. On localhost in VS2017 , it´s running correct, the tests open IE, run the test and then close the IE.
On TFS build server, the tests start IE somehow on background (in Task manager I see two iexplorer.exe processes), but the window of IE is not visible. The tests find elements, but they are not able to write text in textbox, always get error like "Element cannot be interacted with via the keyboard because it is not focusable"
Localy I run Win10 and IE11, TFS build server run Windows Server 2012 R2 and IE11 .
//initialize driver in test constructor
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.RequireWindowFocus = true;
driver = new InternetExplorerDriver(options);
driver.Manage().Window.Maximize();
//test itself
driver.Navigate().GoToUrl(appURL);
var x = driver.FindElement(By.Id("FiltrADuvodDotazu_DuvodDotazu"));
x.SendKeys("Automatizovaný test"); //here I get error
Is there way to run IE visibly, so the tests can interact with it?
I guess your agent run as a service and this is the reason the tests run on "headless mode" (and IE not supports it, like mentioned in the comments).
To solve it you need to configure the agent as an interactive process with auto-logon enabled.
When configuring the agent, select 'No' when prompted to run as a service. subsequent steps then allow you to configure the agent with auto-logon.
More info you can find here.

Selenium: switch tab in RemoteWebDriver with C#

Currently, I'm trying to run my Selenium tests on Safari using Selenium Grid and RemoteWebDriver. This is my setup:
Mac OS Sierra 10.12.6 as a machine for running tests.
Selenium server 3.5.3.
Safari 11.
C# Selenium WebDriver and Selenium Support (latest version).
I'm using port forwarding on my host OS (Windows 10) to forward requests to Mac, running on my Virtual Machine. On my Mac I have Selenium Grid hub, which I run like this:
java -jar selenium-server-standalone-3.5.3.jar -role hub -port 4723
Also, there is a node:
java -jar selenium-server-standalone-3.5.3.jar -role node -hub http://10.0.2.15:4723/grid/register
In code, I start my driver like this:
SafariOprions options = new SafariOptions();
IWebDriver driver = new RemoteWebDriver(new Uri(hubURL), options.ToCapabilities());
My tests are running fine with current setup. But when it comes to clicking a link with attribute target='_blank' I'm starting to face some troubles. For other drivers, which I run locally, I can switch tab without any trouble: I'm getting driver.WindowHandles before I click a link, then I click a link and again retrieve Window Handles to compare with previous handles. After that I use driver.SwitchTo().Window(newHandle) and everything is ok.
But when it comes to RemoteWebDriver (or SafariDriver from Apple, I cannot say more precisely) I'm always getting only one Window Handle, even if the new tab is opened and i can see it.
I'm trying to avoid switching tab with "Command + T" as one of solutions suggested, because my tests are meant to be run on all browsers (Chrome, Firefox, Opera, Edge, Safari) and this wont work.
UPDATE: I've tried running Chrome and other browsers in Selenium Grid via RemoteWebDriver and I can say that this is not an issue of RemoteWebDriver. Next, I've installed Visual Studio for Mac and rewrite several things to run my tests without Selenium Grid, just using this code:
//if memory serves, just like this
SafariOptions options = new SafariOptions();
SafariDriver driver = new SafariDriver(options);
But, unfortunately, this didn't help. Driver navigated me to the page, clicked the link and opened a new tab, but without any switch. When I checked for driver.WindowHandles I've only got one, although there was two visible tabs. Neither driver.SwitchTo().ActiveElement nor driver.SwitchTo().Frame(hardcoded_frame_name) doesn't seem to work. Pretty long waits (for 60 seconds after opening the link and another one after that) aren't working too. Now I think that this is really a bug and I will try to report this to Apple as soon as I can.
But for now, maybe someone has a fancy workaround for this?
As we discussed in the comments, it seems to be a timing issue. So we will induce
WebDriverWait to sync up with the trailing Browser instance. I am providing a code block as a solution through Selenium-Java, consider implementing it in C# and update me if it works for you.
driver.get("http://www.google.com");
System.out.println("Page Title is : "+driver.getTitle());
String parent_window = driver.getWindowHandle();
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
WebDriverWait wait = new WebDriverWait(driver,3);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindows_1 = driver.getWindowHandles();
ArrayList<String> tabs = new ArrayList<>(allWindows_1);
driver.switchTo().window(tabs.get(1));
wait.until(ExpectedConditions.titleContains("Facebook"));
System.out.println("First Child Handle : "+driver.getTitle());
I could help you with the Java version:
After the actions done, do this below.
//Store the parent window
String parentWindow = driver.getWindowHandle();
//Open a new Windows(Mailtrap)
String a = "window.open('https://mailtrap.io/signin','_blank');";
((JavascriptExecutor)driver).executeScript(a);
//This Thread.sleep is useful with Safari. Do not remove.
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Take control over new browser
for(String handle: driver.getWindowHandles()){
driver.switchTo().window(handle);
}

Cannot start the driver service on localhost when using geckodriver with Firefox 50.0.1 in .NET

I'm learning Selenium from the scratch and trying to run a test case on Firefox 50.0.1 using geckodriver I installed in VS2015 by selecting Selenium.WebDriver.GeckoDriver.Win64
However, when running the test I got an exception
Cannot start the driver service on localhost
What am I missing?
I was following some tutorials and performed step by step walk-through.
This is my code:
[TestMethod]
public void WebDriverSample()
{
IWebDriver webDriver;
//IWebDriver webDriver = new InternetExplorerDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
//webDriver = new ChromeDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
webDriver = new FirefoxDriver();
Thread.Sleep(1000);
webDriver.Dispose();
}
When using Firefox 47.0.2, I did not need to use geckodriver at all and it worked just fine.
Now, since browsers are getting updated, at some point I need to start using new versions. So, I need to find out what to do in order to be able to adapt to new changes and use geckodriver
Any suggestons?

Categories