Selenium server start within .NET - c#

I'm using Selenium to control Opera within C#. I'm using selenium-server-standalone-2.33.0 .
When i start the server from command line there is no problem . My code work well.
But I need to start the server from C# and I can start it with a bat file execution. I can start the server and create a driver.
(I'm using "java -jar selenium-server-standalone-2.33.0.jar -trustAllSSLCertificates" command to start the server in both cases.)
My problem is:
If the server started from C# code, my code can't find the element and throwing exception: (Driver's page source property contains xxx element.)
My Code which throws the exception:
element = driver.FindElement(By.Id("xxx"));
All properties of the element throw an exception.
I think it's because of the process.start privileges when i start server. I've searched a lot but i couldn't find anything.
Thanks

I can start sever with this command "java -jar selenium-server-standalone-2.33.0.jar -trustAllSSLCertificates" from command line and C# code. Server starting well i can create driver and can see the page source .
IWebDriver driver = new RemoteWebDriver(new System.Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Opera());
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(100));
But when i want to reach WebElement through this code element throws lots of exception ('element.Displayed' threw an exception of type 'System.InvalidOperationException')
IWebElement element; // FindElement(driver, "txtUserName", 100);
element = driver.FindElement(By.Id("txtUserName"));
string name = element.GetAttribute("Name");

Related

EdgeDriver - Cannot change window size in Edge

I am using the EdgeDriver for running automation tests on my browser (Edge 38.14393.0.0). My tests are in C#, so I am using the .NET driver:
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Edge;
var options = new EdgeOptions();
options.PageLoadStrategy = EdgePageLoadStrategy.Normal;
RemoteWebDriver driver = return new EdgeDriver(Environment.CurrentDirectory, options, TimeSpan.FromSeconds(60));
driver.SetDocumentSize(new Size(800, 600)); // HERE!
The error
This code is the one I run at the beginning of the test. And it fails at the last line with:
Class Initialization method
Web.TestSuite.UIRendering.RenderingTestSuiteEdge.TestClassInitialize
threw exception. System.InvalidOperationException:
System.InvalidOperationException: A window size operation failed
because the window is not currently available.
With this stack trace:
OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs: line 1126
OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs: line 920
OpenQA.Selenium.Remote.RemoteWindow.set_Size(Size value) in ...
FYI Be aware that I have other tests running on Chrome and IE11 using their respective drivers. When I call SetDocumentSize on those, I get no errors.
Open issues
I could find some open issues related to this problem:
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9340417/
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8778306/
Questions
So, these are my questions:
Has anybody succeeded in setting the window size in Edge?
Is this problem I am hitting a known issue? If so, is it fixed? The referenced issues (which look similar) are still open and no status provided.
Is there any workaround?
Try one of those for C#:
driver.Manage().Window.Size = new Size(1920, 1080);
driver.Manage().Window.Maximize();
Although I encounter that error for different reason (like the one here -> https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10319887/).
Now I kill all process of the driver & edge before each test so hopefully it will be resolved like that:
try
{
foreach (var process in Process.GetProcessesByName("MicrosoftWebDriver"))
{
process.Kill();
}
foreach (var process in Process.GetProcessesByName("MicrosoftEdge"))
{
process.Kill();
}
}
catch (Exception)
{
}
Also if you run them on remote machine via RDP for example it will make the same error when you close the RDP. This is the current workaround that I found for it:
Create a batch file with this code:
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
%windir%\System32\tscon.exe %%s /dest:console
)
Create a desktop shortcut to this file. To do this, right-click the batch
file and select Send to | Desktop (create shortcut).
In the shortcut properties, click Advanced and select Run as administrator.
Now, when you need to disconnect from Remote Desktop, double-click this shortcut on the remote computer (in the Remote Desktop window).
Thanks to https://support.smartbear.com/testcomplete/docs/testing-with/running/via-rdp/keeping-computer-unlocked.html for the script.
This works for me:
driver.manage().window().setSize(new Dimension(1250, 720));

Invalid Argument was supplied Exception when running C# test using Selenium WebDriver with Firefox 50.0

I'm learning Selenium WebDriver and trying to run a simple test using Firefox 50 and Webdriver 3.0.1. I'm using Marionette driver 0.11.1
I have the following code:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"P:\Selenium Practice\FirefoxDriver", "wires.exe");
service.FirefoxBinaryPath = #"C:\Program Files\Mozilla Firefox\firefox.exe";
FirefoxOptions options = new FirefoxOptions();
options.AddAdditionalCapability(CapabilityType.AcceptSslCertificates, true);
TimeSpan time = TimeSpan.FromSeconds(10);
IWebDriver driver = new FirefoxDriver(service, options, time);
driver.Navigate().GoToUrl("http://www.demoqa.com");
When running it, I'm getting the following exception with error code 10022:
It says:"An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll"
I'm not really getting what I'm doing wrong. I went throw a lot of suggestions and none of those really worked. The last one that I describe here is that one: Setting up Marionette/ GeckoDriver
What can I do to fix it?

Unable to find an element in Browser of the Android emulator using Appium and C#

I want to automate mobile web site testing on Android emulator using c# and Appium. There is a simple test scenario I want to automate for the start:
1. Start Browser
2. Find an element
3. Clear it
4. Send keys
I've got a problem with the second step. Every time MSTest tries to execute FindElementById line in the code below, I get the error:
"An element could not be located on the page using the given search parameters."
[TestClass]
public class UnitTest1
{
private DesiredCapabilities _capabilities;
private AndroidDriver _driver;
public void InitializeDriver()
{
Console.WriteLine("Connecting to Appium server");
_capabilities = new DesiredCapabilities();
_capabilities.SetCapability("deviceName", "test_02");
_capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
_capabilities.SetCapability(CapabilityType.Version, "5.0.1");
_capabilities.SetCapability(CapabilityType.Platform, "Android");
//Application path and configurations
_driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), _capabilities);
}
[TestMethod]
public void TestMethod1()
{
InitializeDriver();
var element = _driver.FindElementById("com.android.browser:id/url");
element.Clear();
element.SendKeys(#"http://stackoverflow.com/");
}
}
Input string for the method I've got from UIAutomator that is shown below.
I tried several combinations for the FindElementById input method:
"com.android.browser:id/url"
"id/url"
"url"
but no luck.
My environment:
Windows 8.1
Appium 1.3.4.1
ChromeDriver 2.14.313457
Android Device Monitor 24.0.2
Sorry for misleading !!!
In case of testing web apps in browser the elements should be located as usual elements on the web page ( not as some classes like android.widget.EditText and android.widget.Button). So try for example the following and you will see some result:
var element = _driver
.findElementByXPath("//input[#id='lst-ib']");
To get locators you should run the browser on your desktop, open the page and use some tools/extensions like Firebug in Firefox or Firebug Lite in Chrome browser.
Try these 2 statements:
var element = _driver.FindElement(By.Id("com.android.browser:id/url");
driver.findElementsByXPath("//*[#class='com.android.browser' and #index='1']");
Update ! The following approach is not for web testing:
Could you try to find the element using xpath?
#FindBy(xpath="//android.widget.EditText[contains(#resource-id, 'url')]")
So in your case you can try the following:
var element = _driver.findElementByXPath("//android.widget.EditText[contains(#resource-id, 'url')]");
Update: in case of testing web apps (not native) you should use web page locators instead of Android classes.

Remote Webdriver Chrome throws a "path to the driver executable" error

Hi when i use the following code
IWebDriver _webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),
DesiredCapabilities.Chrome());
I get the follwing error
System.InvalidOperationException : The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at Testframework.Browser.RemoteGoto(String browser, String url) in Browser.cs: line 86
at Testframework.CommonAction.RemoteBrowser(String browser) in CommonAction.cs: line 70
at Test.RegistrationTest.InvalidRegistrationTest(String browser, String username, String password, String confirmPassword, String securityQuestion, String securityAnswer, String errorMessageText, String firstname, String lastname) in RegistrationTest.cs: line 50
--TearDown
at Testframework.CommonAction.CaptureScreen(String fileName) in CommonAction.cs: line 121
at Test.RegistrationTest.SnapshotOnFailure() in RegistrationTest.cs: line 590
The clue really is in the error.
Chrome should be installed on the system where the tests are either running on or being pointed to.
Take a step back, look at the documentation:
https://code.google.com/p/selenium/wiki/ChromeDriver
Also, if Chrome is installed in a peculiar place, you'll need to point Selenium to it's location. Again, this is explained in the documentation.
In C#:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.binary", this.binaryLocation);
or:
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = "pathtogooglechrome";
capabilities.SetCapability(ChromeOptions.Capability, options);
Instead of changing the code you can have other way round.
Download the chrome driver and set the PATH environment variable pointing to the directory where the chromedriver.exe is present.
Restart your IDE / Command console and run the tests. It works!!!

Selenium C# RemoteWebDriver not finding XPath Elements

I'm using Selenium 2.25.1 API, and I'm trying to be able to find the elements using RemoteWebDriver(). Except when I try, it just fails to find the element. I've tried several different combinations with no luck and have been looking this up for a few days now.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement WaitForPage = wait.Until<IWebElement>((d) =>
{
return driver.FindElement(By.XPath((String)data));
});
Is my code where it fails. Basically the data variable is an object grabbed from my database. I converted it, and going though the code it comes out perfectly fine. How the difference is, when I used just the browser (i.e. firefox, IE) it works just fine with no errors. But when I use it with RemoteWebDriver(), it throws InvalidOperationException and throws a popup saying it was unable to find the element. (Server did not provide any stacktrace information).
This is usually what I use
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), IEcapa);
When that is used, it just fails everytime.
Any ideas? I am completely puzzled. Anything is welcome and thanks in advance!
I would suggest using an implicit wait instead of an WebDriverWait statement.
WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
IWebElement WaitForPage = driver.FindElement(By.XPath((String)data));
And make sure that the xpath you are getting from the data variable is valid. If possible post an some xpath you get from the database.

Categories

Resources