What is the equivalent C# version of Java Webdriver #Findby? - c#

I'm moving from a Java environment to .NET and need to write Webdriver tests using a page object model.
In Java I would use the following annotation:
#FindBy(linkText = "More details")
WebElement moreDetailsButton;
Please would someone be able to tell me how to define a WebElement using C#? Also, is the PageFactory.initElements used the same way?
Thanks
Steve

Yes, there is a direct translation.
You are looking for FindsBy:
[FindsBy(How = How.LinkText, Using = "More details")]
private IWebElement moreDetailsButton;
As for the PageFactory.initElements, yes, it's a very similar thing in .NET, usually called in the constructor of the Page Object:
public class LoginPage
{
private IWebDriver _driver;
public LoginPage(IWebDriver driver)
{
_driver = driver;
PageFactory.InitElements(_driver);
}
}
Note, that the Selenium project is entirely open source. You can easily see the source the Page Objects 'helper' classes here.

Related

Invoke Google Chrome Navigation from outside chrome, i.e. CMD or C#

Looking for a solution to invoke Navigation in Chrome from outside of chrome by external process.
We have legacy WinForm Software that needs to browse an Angular HTML5 app that requires Chrome to run. (I have no control of this.)
Something along lines of:
Process process = new Process();
process.StartInfo.FileName =
#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
process.StartInfo.Arguments = "google.com" + " --new-window";
process.Start();
And then some way to hook into that process and have the SAME TAB perform navigation.
magic.navigateTab1(www.anothersite.com);
I quite assume its not easily possible, solution may not use Process etc.. Just, anyway to accomplish it from outside chrome?
Whatever solution can't require installing > ~5MB to C# code base.. Ideally, no installation is preferred.
http://cefsharp.github.io/ <- looked into this but this is huge.
Edit 1: Perhaps using something like this..
https://sites.google.com/a/chromium.org/chromedriver/
I used Selenium to achieve that.
First you must add via Nugget the following packages to your solution
Selenium.WebDriver
Selenium.Chrome.WebDriver;
Then reference the following namespaces
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Create the following property on your class root
IWebDriver driver;
On your class constructor add the following code to create a new Chrome Window handled by Selenium
driver = new ChromeDriver();
Then on your buttons add the following
// Switch the action target to the first tab opened on chrome instace handled by Selenium
driver.SwitchTo().Window(driver.WindowHandles.First());
// Go to a given URL
driver.Navigate().GoToUrl("http://www.yourURL.com.br");
Your code should be like that
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace configure
{
public partial class Form1 : Form
{
IWebDriver driver;
public Form1()
{
InitializeComponent();
driver = new ChromeDriver();
}
private void button1_Click(object sender, EventArgs e)
{
driver.SwitchTo().Window(driver.WindowHandles.First());
driver.Navigate().GoToUrl("http://www.yourURL.com.br");
}
}
}
Hope this solve your problem

Properties of IWebElement are not visible in VisualStudio 2013

I use Selenium WebDriver C# and PageFactory (PageObject) pattern to create automated tests. I develop them in Visual Studio 2013.
Before the latest version of Webdriver (now I use 2.47.0) it worked well but now I face the next problem: I can not view properties of my web elements.
Here is how I create and initialize an element:
public class MyPageObj : DriverCover
{
public MyPageObj(IWebDriver driver) : base(driver)
{
//'Driver' is a EventFiringWebDriver (IWebDriver) from parent class
PageFactory.InitElements(Driver, this);
}
[FindsBy(How = How.CssSelector, Using = "#internal")]
private IWebElement _myElement;
}
When I run tests and at a breakpoint I want to watch properties of _myElement I see only non-public members: http://prntscr.com/8o075p
If I don't use PageFactory and find elements using Driver.FindElement(By.How()) method it works well.
Well as I found in this commit https://github.com/SeleniumHQ/selenium/commit/689276b5e39c414edccf8a1bd11d71fd81b5fad4 were made some changes and that's why my problem is not a bug and won't be fixed(

InternetExplorer - RemoteWebDriver, how to specify the path to the executable file?

I've been trying to get my Grid working with IE, but at some point I need to specify the full path to my driver's exe.
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
DesiredCapabilities ieCapability = DesiredCapabilities.InternetExplorer();
ieCapability.SetCapability(ieOptions.EnsureCleanSession.ToString(), true);
ieCapability.SetCapability(ieOptions.BrowserCommandLineArguments.ToString(), "-private");
I need to know if this can be done via InternetExplorerOptions or DesiredCapabilities or if I just should create an environment variable holding the path to my IE driver.
Thanks!
One of the most common practices i've seen in my Selenium experience, is testers will put their statement in their super class.
Consider the following (I'm a Java programmer, so this will be java code, you can convert to C#):
public class SuperTest {
public SuperTest() {
System.setProperty("webdriver.ie.driver", "C:\\Path\\To\\IEDriver.exe");
}
}
public class Test extends SuperTest {}
You can see how you can do it in the Getting started with Selenium framework (again, Java).

How to populate the fields in browser using Java or C#

As I have a web form I need to fill in repeatedly, can I construct a Java or C# program to populate the form automatically? For example below is a sample contact us form, I wish that when I click on the Java or C# program, the form could be filled in automatically. How to achieve this?
maybe you need to use some autotesting techology. For example: selenium (Java), watir (Ruby), watin (.net).
Those tool provide browser abstraction that help manipulate with page and controls on it.
There is little example:
package selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class ExampleSearch {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
// Open Google
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("selenium best practices");
// Send form with element
element.submit();
}
}
JavaScript is a much better way to do this. Use the right tool for the right job.

Using Selenium 2's IWebDriver to interact with elements on the page

I'm using Selenium's IWebDriver to write Unit Tests in C#.
Such is an example:
IWebDriver defaultDriver = new InternetExplorerDriver();
var ddl = driver.FindElements(By.TagName("select"));
The last line retrieves the select HTML element wrapped in a IWebElement.
I need a way to simulate selection to a specific option in that select list but I can't figure out how to do it.
Upon some research, I found examples where people are using the ISelenium DefaultSelenium class to accomplish the following, but I am not making use of this class because I'm doing everything with IWebDriver and INavigation (from defaultDriver.Navigate()).
I also noticed that ISelenium DefaultSelenium contains a ton of other methods that aren't available in the concrete implementations of IWebDriver.
So is there any way I can use IWebDriver and INavigation in conjunction with ISelenium DefaultSelenium ?
As ZloiAdun mentions, there is a lovely new Select class in the OpenQA.Selenium.Support.UI namespace. That's one of the best ways to access a selection element and it's options because it's api is so easy. Let's say you've got a web page that looks something like this
<!DOCTYPE html>
<head>
<title>Disposable Page</title>
</head>
<body >
<select id="select">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
You're code to access the select would look like this. Note how I create the Select object by passing a normal IWebElement to it's constructor. You have plenty of methods on the Select object. Take a look at the source for more information, until it gets properly documented.
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Collections.Generic;
using OpenQA.Selenium.IE;
namespace Selenium2
{
class SelectExample
{
public static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("www.example.com");
//note how here's i'm passing in a normal IWebElement to the Select
// constructor
Select select = new Select(driver.FindElement(By.Id("select")));
IList<IWebElement> options = select.GetOptions();
foreach (IWebElement option in options)
{
System.Console.WriteLine(option.Text);
}
select.SelectByValue("audi");
//This is only here so you have time to read the output and
System.Console.ReadLine();
driver.Quit();
}
}
}
A couple things to note about the Support class however. Even if you downloaded the latest beta, the support DLL won't be there. The Support package has a relatively long history in the Java libraries (that's where PageObject lives) but it's still pretty fresh in the .Net driver. Fortunately, it's really easy to build from source. I pulled from SVN then referenced the WebDriver.Common.dll from the beta download and built in C# Express 2008. This class hasn't been as well tested as some of the other classes, but my example worked in Internet Explorer and Firefox.
There's a few other things that I should point out based on your code above. Firstly the line you were using to find the select element
driver.FindElements(By.TagName("select"));
is going to find all select elements. you should probably use driver.FindElement, without the 's'.
Also, very rarely would you use INavigation directly. You'll do most of your navigation like driver.Navigate().GoToUrl("http://example.com");
Lastly, DefaultSelenium is the way to access the older Selenium 1.x apis. Selenium 2 is a pretty significant departure from Selenium 1, so unless you're trying to migrate old tests to the new Selenium 2 api (often referred to as the WebDriver api) you won't use DefaultSelenium.
You should get all option elements from your select using ddl.FindElements(By.TagName("option"));. Then you can iterate through the returned collection and select required item(s) by using SetSelected method of the IWebElement
Update: It seems that there's now a C# implementation of the Select in WebDriver - previously it was in Java only. Please take a look at its code and it is easier to use this class

Categories

Resources