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.

 

Using different emulators with Chrome in C#

Hi guys, Today I will post a sample code that you can use to test different screens/mobile devices/browsers using Google Chrome browser with C#. You will need change the bold words for the devices and sizes that you want to simulate.

private static ChromeOpt SetCapabilities()
{
Map chromeOptions = new HashMap();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();  capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);


switch (whichCase) {


case "byDevice":
Map mobileEmulation = new HashMap();
mobileEmulation.put("deviceName", "Apple iPhone 4"); //Should be the same name showing in the Chrome Device Emulator
chromeOptions.put("mobileEmulation", mobileEmulation);
break;

case "CustomScreen":
Map deviceMetrics = new HashMap();
deviceMetrics.put("width", 900);
deviceMetrics.put("height", 600);
deviceMetrics.put("pixelRatio", 3.0);


Map mobileEmulation = new HashMap();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
chromeOptions.put("mobileEmulation", mobileEmulation);


break;

}

}
IWebDriver driver =new ChromeDriver(capabilities);
return driver;
}

You can look the list of user agents here:

http://www.useragentstring.com/pages/useragentstring.php

If you have any thoughts/suggestions please leave your comment here 🙂

Thank you guys !

Insert data in your DataBase

Hello, after you have created your database and connected in it, you can input some data in the middle of the programming.

Like this example:

1: public class Program
2: {
3: static Person GetPerson()
4: {
5: Person person = new Person();
6: Console.WriteLine(“Name”);
7: person.Name = Console.ReadLine();
8: Console.WriteLine(“Email “);
9: person.Email = Console.ReadLine();
10: Console.WriteLine(“Gender (M ou F) “);
11: person.Gender= Convert.ToChar(Console.ReadLine());
12: Console.WriteLine(“Date of Born”);
13: person.DateofBorn= Convert.ToDateTime(Console.ReadLine());
14: return person;
15: }
16:
17: static void Main(string[] args)
18: {
19: //
20: //calls a method that will fill out a object Person with users input
21: //
22: Person newPerson = GetPerson();
23:
24: //
25: //string of connection that reports datas of DataBase that I will connect
26: //
27: string connectionString = @”Data Source=.\SQL;Initial Catalog=EstudyBlog;Integrated Security=True;Pooling=False”;
28:
29: //
30: // Query TSQL with command that I will realize in DataBase
31: //
32: string query = “INSERT INTO Person (name, dateBorn, gender, email) values (@name, @dateBorn, @gender, @email)”;
33:
34: SqlConnection conn = null;
35: try
36: {
37: //
38: //instance of connection
39: //
40: conn = new SqlConnection(connectionString);
41:
42: //
43: //look for if the connection is close, if it is so it will open.
44: //
45: if (conn.State == ConnectionState.Closed)
46: {
47: //
48: //open connection
49: //
50: conn.Open();
51: }
52:
53: //
54: // Creat of object command , that receives a query that will used in operation and the connection with tha DataBase.
55: //
56: SqlCommand cmd = new SqlCommand(query, conn);
57:
58: //
59: // Add parameters in command
60: //
61: cmd.Parameters.Add(new SqlParameter(“name”, newPerson.Name));
62: cmd.Parameters.Add(new SqlParameter(“dateBorn”, newPerson.DateBorn));
63: cmd.Parameters.Add(new SqlParameter(“gender”, newPerson.Gender));
64: cmd.Parameters.Add(new SqlParameter(“email”, newPerson.Email));
65:
66: //
67: // Execute command
68: //
69: cmd.ExecuteNonQuery();
70:
71: //
72: // Close the connection with DataBase
73: //
74: conn.Close();
75:
76: Console.WriteLine(“Person registered with success!!!”);
77: }
78: catch (Exception ex)
79: {
80: Console.WriteLine(ex.Message);
81: }
82: finally
83: {
84: //
85: // Ensures that the connection will be closed also that ocorres some error.
86: // Don’t exist problem in close a connection two times.
87: // The problem is in open a connection two times.
88: //
89: if (conn != null)
90: {
91: conn.Close();
92: }
93: }
94: }
95: }

Good studys for us !

Font: http://fpimentel88.wordpress.com/2009/01/18/aprendendo-c-parte-1-acessando-um-banco-sql-server-2005-com-adonet/

LINQ to SQL Overview

Hello again, happy new year!
In this video, you will heard how insert, select and delete some lines utilizing C# in databate of SQL.


Any question ? It’s very simple when you can follow the steps of this video.

Bye bye !

Remember again about Classes

We define classes how groups of objects than they are grouped for have equallities of comportment and features and of differ structure of data, because they can duplicates and inherit forming the new classes.
For understanding the classes, we consider the need of to create a object CARRO, will list the features of a CARRO :

Name of object: Carro (model)
Features: Cor, marca (attributes)

Now, we see how declare a class in C#:

Class Carro
Public sCor As String
Public sMarca As String
End Class
But a class of will care all details of a car will are more very complex, it will have details of tires, engine, opcionals. For this, there is a inherited and the polymorphism for to easy the creation of new objects. See:

Class Pneu
Public iTamanho As Integer
Public iLargura As Integer
End Class

Class Carro
Inherits Pneu

Public sCor As String
Public sMarca As String
End Class

Class Caminhao
Inherits Pneu

Public sCor As String
Public sMarca As String
End Class

Class Onibus
Inherits Pneu

Public sCor As String
Public sMarca As String
End Class

Font: http://www.csharpbr.com.br/Portal/VisualizarArtigo.aspx?Artigo=64