Your Ultimate Cheat Sheet to work with Selenium Webdriver

by:

ProgrammingResourcesTips & Tricks

Have you been thinking to start learning or improving your skills in Selenium? Here are few tips and tricks that could help you a lot while working with Selenium.

1. Taking a Screenshot

You can use the following method to take a full-screen screenshot of the browser.


public void TakeFullScreenshot(IWebDriver driver, String filename)
{
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(filename, ImageFormat.Png);
}

Sometimes, you may need to take a screenshot of a single element.


public void TakeScreenshotOfElement(IWebDriver driver, By by, string fileName)
{
// 1. Make screenshot of all screen
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));

 


// 2. Get screenshot of specific element
IWebElement element = driver.FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
bitmap.Save(fileName);
}

First, we make a full-screen screenshot, then we locate the specified element by its location and size attributes. After that, the found rectangle chunk is saved as a bitmap.

Here is how you use both methods in tests.


[TestMethod]
public void WebDriverAdvancedUsage_TakingFullScrenenScreenshot()
{
this.driver.Navigate().GoToUrl(@"http://skillholic.com");
this.WaitUntilLoaded();
string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
this.TakeFullScreenshot(this.driver, tempFilePath);
}

 


[TestMethod]
public void WebDriverAdvancedUsage_TakingElementScreenshot()
{
this.driver.Navigate().GoToUrl(@"http://skillholic.com");
this.WaitUntilLoaded();
string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
this.TakeScreenshotOfElement(this.driver,
By.XPath("//*[@id='tve_editor']/div[2]/div[2]/div/div"), tempFilePath);
}

We get a temp file name through the special Path .NET class. By default, temp files are generated with “.tmp” extension because of that we replace it with “.png”.

 

skillholic-rectangle-selenium_768_90

Want to learn Selenium in an Instructor-led online session? Learn Selenium from Live Expert.

 

2. Get HTML Source of WebElement

You can use the GetAttribute method of the IWebElement interface to get the inner HTML of a specific element.


[TestMethod]
public void GetHtmlSourceOfWebElement()
{
this.driver.Navigate().GoToUrl(@"http://skillholic.com");
this.WaitUntilLoaded();
var element = this.driver.FindElement(By.XPath("//*[@id='tve_editor']/div[2]/div[3]/div/div"));
string sourceHtml = element.GetAttribute("innerHTML");
Debug.WriteLine(sourceHtml);
}

 

3. Use Specific Profile

By default WebDriver always assigns a new ‘clean’ profile if you use the FirefoxDriver’s default constructor. However, sometimes you may want to fine-tune the profile e.g. add extensions, turn off the JavaScript, etc.


FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("YourProfileName");
this.driver = new FirefoxDriver(profile);

You can do some similar configurations for the ChromeDriver.


ChromeOptions options = new ChromeOptions();
// set some options
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);

 

4. Turn Off JavaScript

You can use the code from point 7 to set up a new Firefox profile. Then you need to set the ‘javascript.enabled’ attribute to false.


FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("HARDDISKUSER");
profile.SetPreference("javascript.enabled", false);
this.driver = new FirefoxDriver(profile);

 

5. Maximize Window

Use the Maximize method of the IWindow interface.


[TestMethod]
public void MaximizeWindow()
{
this.driver.Navigate().GoToUrl(@"http://skillholic.com");
this.driver.Manage().Window.Maximize();
}

 

Want to learn Selenium in an Instructor-led online session? Learn Selenium from Live Expert.

 

6. Change User Agent

Once you know how to create a new custom Firefox profile, you can set its argument ‘general.useragent.override’ to the desired user agent string.


FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference(
"general.useragent.override",
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+");
this.driver = new FirefoxDriver(profile);

 

7. Set HTTP Proxy for Browser

Similar to the user agent configuration, to set a proxy for Firefox, you only need to set a few arguments of the Firefox’ profile.



FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("network.proxy.type", 1);
firefoxProfile.SetPreference("network.proxy.http", "myproxy.com");
firefoxProfile.SetPreference("network.proxy.http_port", 3239);
driver = new FirefoxDriver(firefoxProfile);

 

8. Execute JavaScript

You can use the interface IJavaScriptExecutor to execute JavaScript through WebDriver.


[TestMethod]
public void ExecuteJavaScript()
{
this.driver.Navigate().GoToUrl(@"http://skillholic.com");
this.WaitUntilLoaded();
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");
Debug.WriteLine(title);
}

 

9. Doing Drag & Drop Interactions

The most convenient and robust way to perform drag and drop interaction using WebDriver is to use its Action Builder API, and the “DragAndDrop” and “DragAndDropToOffset” methods. The first one accepts a source and a target element as parameters, while the second one moves the source element by an X and Y offsets. Here are two examples:


Actions actions = new Actions(driver);
actions.DragAndDrop(header, groupByArea).Release().Perform();
builder.DragAndDropToOffset(colHeader, offsetX, offsetY).Release().Perform();

You can also do a more low-level drag and drop implementation by executing all the mouse actions that are taking place one by one, again using the Actions class. For instance you can chain calls to ClickAndHold(), MoveByOffset(), and Release(). But for 99 % of the cases, DragAndDrop/ DragAndDropToOffset should work perfectly fine.

10. Switching between windows and iframes

The easiest way to change to a particular iframe/window is to execute:


driver.SwitchTo().Frame(frameElement); //IWebElement

Or (for window):


driver.SwitchTo().Window(handle); // where handle is a member of driver.WindowHandles

And then to switch back to the main window in the case of iframes:


driver.SwitchTo().DefaultContent();

Note that after you change the window or iframe, any method that you call on the driver after that will be executed in the new context, until you switch to a different iframe / window.

 

Want to learn Selenium in an Instructor-led online session? Learn Selenium from Live Expert.

 

Has something to add to the article? Share your thought in comments.

Comments

comments

Comments are closed.