Home > Programming, Programming, QA, Test and Automation Stuffs > Open different browsers with C# and Selenium

Open different browsers with C# and Selenium

Hey guys, I am going to post some snippets to run a test in different browsers with C# and selenium.

Import:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.PhantomJS;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium.Support.PageObjects;
using Assert = NUnit.Framework.Assert;

 

Driver Utils class: 

Just the beginning of the class declaring the WebDriver and the TxtSearch field, which will be used for all the below functions to open a specific browser.

namespace Helpers
{
 [TestClass]
 public class DriverUtils
 {
 IWebDriver webDriver;

 //Declaring search element
 [FindsBy(How = How.Name, Using = "q")]
 public IWebElement TxtSearch { get; set; }

 

Open Chrome:

 [Test]
 public void Chrome_Browser()
 {
 webDriver = new ChromeDriver();
 AssertSearchElement();
 }

 

Open Chrome Mobile Emulator:

 [Test]
 public void Chrome_Mobile_Emulator_Browser()
 {
 ChromeOptions chromeCapabilities = new ChromeOptions();
 chromeCapabilities.EnableMobileEmulation("Google Nexus 5");
 webDriver = new ChromeDriver(chromeCapabilities);
 AssertSearchElement();
 }

 

Open Chrome Capabilities starts Maximized:

 [Test]
 public void Chrome_Capabiblities_Browser()
 {
 ChromeOptions chromeCapabilities = new ChromeOptions();
 chromeCapabilities.AddArgument("start-maximized");
 webDriver = new ChromeDriver(chromeCapabilities);
 AssertSearchElement();
}

 

Open Firefox:

[Test]
 public void Firefox_Browser()
 {
 webDriver = new FirefoxDriver();
 AssertSearchElement();
}

 

Open Firefox with Profile:

[Test]
 public void Firefox_Profile_Browser()
 {
 var profile = new FirefoxProfile();
 profile.SetPreference("browser.startup.homepage", 
"https://www.azevedorafaela.wordpress.com/");
 webDriver = new FirefoxDriver(profile);
 AssertSearchElement();
}

 

Open PhantomJS:

[Test]
 public void PhantomJS_Browser()
 {
 webDriver = new PhantomJSDriver();
 AssertSearchElement();
}

 

Open PhantomJS with Capabilities:

[Test]
 public void PhantomJS_Capabilities_Browser()
 {
 var options = new PhantomJSOptions();
 options.AddAdditionalCapability("phantomjs.page.settings.userAgent", 
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/40.0.2214.94 Safari/537.36");
 webDriver = new PhantomJSDriver(options);
 AssertSearchElement();
}

 

Open Internet Explorer:

 [Test]
 public void IE_Browser()
 {
 webDriver = new InternetExplorerDriver();
 AssertSearchElement();
}

 

Open Internet Explorer with Options:

[Test]
 public void IE_Options_Browser()
 {
 var options = new InternetExplorerOptions
 {
 IgnoreZoomLevel = true 
 };

 webDriver = new InternetExplorerDriver(options);
 AssertSearchElement();
}

 

Assert Search Element is Displayed:

public void AssertSearchElement()
 {
 webDriver.Navigate().GoToUrl("https://www.google.com");

 PageFactory.InitElements(webDriver, this);
 Assert.IsTrue(TxtSearch.Displayed);
}

 

Close/Quit Browser and driver function:

[TearDown]
public void CloseDriver(IWebDriver driver)
  {
  driver.Close();
  driver.Quit();
  }
 }
}

 

See you again in the end of this week, hopefully I will have the complete flow for a BDD scenario with C# and Selenium.

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: