Total Pageviews

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, 12 January 2015

ChromeDriver: Mobile Emulation

ChromeDriver is a standalone server which implements WebDriver's wire protocol.

[C#]

    var mobileEmulation = new Dictionary
    {
        { "deviceName", "Apple iPhone 5" }
    };
    var options = new ChromeOptions();
    options.AddAdditionalCapability("mobileEmulation", mobileEmulation);
    driver = new ChromeDriver(options);
  
Tips:
  • The “mobileEmulation” dictionary must use a valid device name from the DevTools Emulation panel.  
    
 
Reference:
 
ChromeDriver
Provides a mechanism to write tests against Chrome 

ChromeOptions
Class to manage options specific to ChromeDriver

AddAdditionalCapability
Provides a means to add additional capabilities not yet added as type safe options for the Chrome driver. 

Wednesday, 26 March 2014

Selenium WebDriver: Say cheese

Taking screenshots of a test failures using Selenium WebDriver can come in handy, especially when investigating failed test cases from previous test executions. For example, a test that may have ran overnight.
 
Screenshots coupled with the stack trace and logging allows you to diagnose why a test failed without the need to rerun the test.

In the solution below I will detail how to take a screenshot using Selenium WebDriver, with the following caveats;
  • I am not sure what test may fail, so I cannot explicitly set what tests should take a screenshot.
  • I do not want to capture a screenshot of successful test runs.
 
To get around this problem I will be taking advantage of NUnit's TestContext which contains a wealth of useful information. For example, the full name and status of a test.

Tips:


Ensure you edit the location for the screenshot file as you see fit.

[C#]
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace TestAutomationEngineer
{
    [TestFixture]
    public class Screenshots
    {
        private IWebDriver driver;
        private string baseURL;
        private string filename;
        private string saveLocation;
        
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://testautomationengineer.blogspot.co.uk/";
        }
        
        [TearDown]
        public void TeardownTest()
        {
         if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
         {
          Snap(driver);
          Console.WriteLine("Check out why the test failed, look here: " + filename);
         }
         
         if(driver != null)
         {
          driver.Quit();
         }
        }
        
        [Test]
        public void TakeScreenshotTestThatPasses()
        {
         driver.Navigate().GoToUrl(baseURL);
         Assert.IsTrue(true);
        }
        
        [Test]
        public void TakeScreenshotTestThatFails()
        {
         driver.Navigate().GoToUrl(baseURL);
         Assert.IsTrue(false);
        }
        
        public void Snap(IWebDriver driver)
        {
         try{
          createTempDirectory();
          createFilename();
    ((ITakesScreenshot) driver).GetScreenshot().SaveAsFile(filename, ImageFormat.Png);
         }
         catch(Exception){}
        }
        
        public void createTempDirectory(){
         try{
          saveLocation = "c:\\temp\\";
    bool dirExists = System.IO.Directory.Exists(saveLocation);
    if(!dirExists)
        System.IO.Directory.CreateDirectory(saveLocation);
         }
         catch(Exception){}
        }
        
        public void createFilename(){
      try{
    var timeStamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
    filename = TestContext.CurrentContext.Test.FullName;
    string ext = ".png";
    filename = filename + timeStamp;
    filename = saveLocation + filename + ext;
         }
         catch(Exception){}
        }
        
    }
}

Reference:

ITakesScreenshot
Defines the interface used to take screen shot images of the screen.

GetScreenshot
Gets a Screenshot object representing the image of the page on the screen.

TestContext (NUnit)
The TestContext class allows tests to access certain information about the execution context.


Saturday, 28 December 2013

Selenium WebDriver: Handling mouseover and hover events

There are many ways to simulate mouse movement in Selenium WebDriver. A few of the of the ways I have used to date can be seen below.
  1. Use the Actions class
  2. Hijack the mouse
  3. Execute Javascript
Example

Hello world



[C#] Solution 1 : Action class

[Test]
public void handlingMouseoverEvent()
{
driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/12/selenium-webdriver-handling-mouseover.html");
Actions builder = new Actions(driver);
IWebElement smiley = driver.FindElement(By.Id("hoverOverMe"));
builder.MoveToElement(smiley).Build().Perform();
}


[C#] Solution 2 : Hijack the mouse

[Test]
public void handlingMouseoverEvent()
{
driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/12/selenium-webdriver-handling-mouseover.html");
ILocatable hoverItem = (ILocatable) driver.FindElement(By.Id("hoverOverMe"));
IMouse mouse = ((IHasInputDevices) driver).Mouse;
mouse.MouseMove(hoverItem.Coordinates);
}


[C#] Solution 3 : Execute Javascript

[Test]
public void handlingMouseoverEvent()
{
driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/12/selenium-webdriver-handling-mouseover.html");
var js = (IJavaScriptExecutor)driver;
            js.ExecuteScript("$('[id=hoverOverMe]').trigger('mouseover')");
}


Reference:

Actions
Provides a mechanism for building advanced interactions with the browser.

ExecuteScript
Executes JavaScript in the context of the currently selected frame or window.

IJavaScriptExecutor
Defines the interface through which the user can execute JavaScript.

ILocatable
Defines the interface through which the user can discover where an element is on the screen.

IMouse
Provides methods representing basic mouse actions.

MouseMove(ICoordinates)
Moves the mouse to the specified set of coordinates.

MoveToElement(IWebElement)
Moves the mouse to the specified element.

Monday, 25 November 2013

Selenium WebDriver: Perform a drag and drop

If your browser supports advanced user interactions like drag and drop, you can use the Actions class to imitate this behaviour.

Drag and Drop

This is an interactive demo. Drag and drop the small square into the large square. 



Below you will find an example detailing how to drag one element to another:

[C#] Solution 1

  [Test]
  public void draganddrop()
  {
   driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/11/selenium-webdriver-perform-drag-and-drop.html");
   
   IWebElement source = driver.FindElement(By.Id("draggable"));
   IWebElement target = driver.FindElement(By.Id("droppable"));
   
   Actions builder = new Actions(driver);
    builder.DragAndDrop(source, target);
    builder.Build().Perform();

    Assert.That(dragAndDropIsComplete());
  }
  
  private bool dragAndDropIsComplete()
  {
   return driver.FindElement(By.ClassName("drag-and-drop-complete")).Displayed;
  }

[C#] Solution 2


  [Test]
  public void draganddrop()
  {
   driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/11/selenium-webdriver-perform-drag-and-drop.html");
   
   IWebElement source = driver.FindElement(By.Id("draggable"));
   IWebElement target = driver.FindElement(By.Id("droppable"));
   
   Actions builder = new Actions(driver);
          builder.ClickAndHold(source);
          builder.MoveToElement(target);
          builder.Release(target);
          builder.Build().Perform();
    
   Assert.That(dragAndDropIsComplete());
  }
  
  private bool dragAndDropIsComplete()
  {
   return driver.FindElement(By.ClassName("drag-and-drop-complete")).Displayed;
  }

Tips:

  • Ensure native events are enabled
  • Maximise your browser windows before you perform a drag and drop


Reference: 

DragAndDrop

Performs a drag-and-drop operation from one element to another.

ClickAndHold

Clicks and holds the mouse button down on the specified element.

MoveToElement

Moves the mouse to the specified element.

Tuesday, 12 November 2013

Selenium WebDriver: Handling multiple windows

When testing web applications you may come across a few situations when you will need to handle multiple windows.

A common example of this could be clicking on a hyper-link which subsequently opens a new web-page in another window.

In some cases this may be a problem because Selenium WebDriver does not automatically switch to the new window.

Link
Visit my blog testautomationengineer.blogspot.co.uk

Below you will find an example detailing how to handle multiple windows:

[C#]

        [Test]
        public void OpenLinkInANewWindowThenSwitchToNewWindow()
        {
         driver.Navigate().GoToUrl("http://testautomationengineer.blogspot.com/2013/11/webdriver-handling-multiple-windows.html");
         
         // get the current windows handle
         string oldWindow = driver.CurrentWindowHandle;
         string newWindow = null;
         
         // open a link in a new window
         IWebElement element = driver.FindElement(By.Id("visitMyBlog"));
         element.SendKeys(Keys.Shift + Keys.Return);
         
         // wait for the new window
         WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
         wait.Until((d) => driver.WindowHandles.Count==2);
            
   // get the new window handle         
         var windowHandles = driver.WindowHandles;
         ReadOnlyCollection<string> windows = new ReadOnlyCollection<string>(windowHandles);
         foreach (string window in windows)
         {
          if(window != oldWindow)
          {
           newWindow = window;
          }
         }
         
         // switch to the new window
         driver.SwitchTo().Window(newWindow);
        }
Tips:

  • It is just as useful to keep track of your original window, as it is to keep track of your new window.
  • Each window in Selenium WebDriver has a unique handle identifier; this allows you to differentiate windows.
  • There may be a delay opening the new window, to avoid problems in this area wait for the count of window handles to increase.

Reference:

Gets the current window handle, which is an opaque handle to this window that uniquely identifies it within this driver instance.

Gets the window handles of open browser windows.