-
Notifications
You must be signed in to change notification settings - Fork 50
Golem.WebDriver, Getting Started
===== ###Guide Navigation Creating a Test
###Selenium WebDriver C# Framework
Selenium WebDriver is collection of language specific bindings used to drive browser actions natively the way a real user would. Combined with the Golem’s Core functionality, you now have a powerful tool to create, and execute, robust, browser-based test automation scripts. By default, the framework will automatically launch Firefox on your local browser before each test, and instantiates a WebDriver instance. Controlling which browsers to use, can easily be modified via the Config class, or an App.Config file.
Golem includes a variety of "features" that get started automatically. For example, having each WebDriver command written to the test log.
####Creating a Test
- Create a new Class Library File -> New -> Project -> Class Library.
- Right click on references -> Manage NuGet Packages
- Add Package “ProtoTest.Golem”.
- WebDriver is available via "driver" property.
- Paste the following code into your class file, save the project/solution, and build:
using MbUnit.Framework;
using ProtoTest.Golem.Tests.PageObjects.Google;
using ProtoTest.Golem.WebDriver;
namespace ProtoTest.Golem.Tests
{
class Test : WebDriverTestBase
{
[Test]
public void TestGoogle()
{
OpenPage<GoogleHomePage>("http://www.google.com/").SearchFor("Selenium").VerifyResult("Selenium – Web Browser Automation");
}
}
}- TestDriven.net plugin is required
- Right Click on Test - > Run Test (Should open browser and close it)
- Right Click on Class or project to run all tests within.
- Open Gallio Icarus
- Click "Add" and navigate to our ProjectName.dll file located in the /bin/release or /bin/debug/ directory.
- Select tests to execute.
- Click Run.
- Add an "Application Configuration File".
- Edit the App.config -> Set browser using key “Browser1” value=”Firefox”
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Browser1" value="Firefox"/>
</appSettings>
</configuration>- Execute Test - > Should open Firefox instead of Chrome.
- Supports Firefox, Chrome, IE, Safari, HtmlUnit, and Android (remote only)
[FixtureInitializer]
public void init()
{
Config.Settings.httpProxy.startProxy = false;
Config.Settings.httpProxy.useProxy = false;
}
####Building a new Page Object:
- Look in the Tests/PageObjects directory in the Golem repository for examples
- Page Objects Inherit BasePage
- Instances of Element represent elements in the web page under test
- The Element API provides convenient methods for locating elements
- WebDriver API available through 'driver' property;
- WaitForElements method is called when page object is instantiated. Use it to wait for dynamic elements.
using OpenQA.Selenium;
using ProtoTest.Golem.WebDriver;
namespace ProtoTest.Golem.Tests.PageObjects.Google
{
public class GoogleHomePage : BasePageObject
{
Element searchField = new Element("SearchField", By.Name("q"));
Element googleLogo = new Element("GoogleLogo", By.Id("hplogo"));
Element searchButton = new Element("SearchButton", By.Name("btnK"));
Element feelingLuckyButton = new Element("ImFeelingLuckyButton", By.Name("btnI"));
Element signInButton = new Element("SignInButon", By.LinkText("Sign in"));
public GoogleResultsPage SearchFor(string text)
{
searchField.Text = text;
searchField.Submit();
return new GoogleResultsPage();
}
public override void WaitForElements()
{
searchField.Verify().Present();
googleLogo.Verify().Present();
searchButton.Verify().Present();
feelingLuckyButton.Verify().Present();
signInButton.Verify().Present();
}
}
}####Executing on a Remote Computer
- Modify App Config, set host to IP or Hostname of remote computer
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Browser1" value="Chrome"/>
<add key="RunOnRemoteHost" value="True"/>
<add key="HostIp" value="10.10.1.151"/>
</appSettings>
</configuration>####Configurable Features:
- Multi Threaded Execution. Mark test with [Parallelizable] attribute. Set number of threads with "DegreeOfParallelism", "1"
- Automatically Launch Browser - LaunchBrowser", "True"
- Specify up to five browsers - "Browser1" value="Firefox"
- Add a delay between commands - "CommandDelayMs" value="0"
- Run on local or remote computer - "RunOnRemoteHost" value="false", "HostIp" value="localhost"
- Capture screenshot on error - "ScreenshotOnError" value="True"
- Capture page html source on error - "HtmlOnError" value="True"
- Capture screen video recording on error - "VideoRecordingOnError", "True"
- Write all webdriver commands to the log - "CommandLogging" value="True"
- Write all page object functions to the log - "ActionLogging" value="True"
- Launch a proxy to capture http traffic - "StartFiddlerProxy" value="True", "ProxyPort" value="8876"
- Appium support - "LaunchApp", "False" - "AppPath", "" - "AppPackage", "" - "AppActivity", "" - "AppOs", "Android"
- Configurable Test timeout - "TestTimeoutMin","5"
- Configurable element Timeout - "ElementTimeoutSec","20"
- Configurable environment Url - "EnvironmentUrl",""
- Automatically check spelling on each page -
###Things to know :
- Multi-threaded support (Parallel test execution)
- Selenium GRID/SauceLabs supported.
- Extended WebDriver API to add additional commands.
- Using EventDrivenWebDriver to log commands.
- Fully configurable programmatically or with an App.Config. Config class reads the App.config and stores all values as properties. Each value should have a default defined.
- Built on the Page Object Design Pattern. Page Objects should inherit BasePageObject
- Test class should inherit TestBaseClass, and each test function should be marked with the [Test] attribute.
- Optional Element class wraps and hides WebDriver API
- WebDriver driver is instantiated automatically for each test, and stored in a static IDictionary in the TestBaseClass. This is for multi-threaded support. Each thread automatically knows which driver belongs to it. Access it in any test or page object like "driver.FindElement".
- Page Objects need to define a WaitForElements() function. This is called automaticalled when the PO is instantiated. You should put waits or validations in here to wait or validate that a page object is fully loaded and ready to be used.
- Page Objects don't need a constructor. The base constructor instantiates everything, and the driver object is stored statically in the TestBaseClass
- You can use MbUnit.Framework.Assert to make assertions. These will stop the test on failure.
- Created a variety of Verifications in the WebDriver API. These will not stop the test if they fail. '
- Supports data driven testing through MbUnit attributes.