DevOps | Software Automation | Continuous Integration

Category: Selenium Automation (Page 2 of 3)

The Art of Test Automation

There is no one or absolute way for building test automation. I use the following approach to choose the technology or tool I use to build my test automation framework:

  • Open source
  • Most popular in the community
  • Regular maintenance by community
  • Able to solve my problem
Below is an example of the approach or tools I use to write a regression test suite for a customised exporter script in my company.
The behaviour of the exporter script is as below:
  • Log into a web interface
  • Fill in details as SQL statement to be run into a web form
  • Kicking off the exporter script via web form
  • Exporter script will execute the SQL statement and generate the output as text file
  • The text file will be sent to a FTP account
Below is how I build the test automation suite:
  • SpecFlow

I use this BDD tool for user readability purpose to explain the flow of the test scenarios

  • Selenium web driver in .NET

This is used to do the web interface interactions such as log in, filling in SQL details, and kicking off the tests

  • WinSCP

To log into the FTP server

  • Batch script

To run WinSCP.exe in order to delete and download files from FTP server

  • C# code

Using the Process class to run the batch script. It is also used to verify and display the output.

  • NUnit

To verify output

Below is the example of going down into each level of 1 step in the scenario:
The SpecFlow scenario looks like this:
Scenario: Kicking off PageUp Exporter
    Given I delete the output file
When I log into Feature Manager
And I kick off an Exporter process with query “SELECT * from dbo.applicant where semail like ‘%test%’ and linstid = ‘543’”
Then I verify that the file is exported
And we verify that the file has “60” rows
And we verify that the file has “100” delimiters
And we verify that the email address contains “test”
And we verify that the file size is “19484”

The step definitions for the 1st step consists of C# code that calls the batch file:

 [Given(@”I delete the output file”)]
        public void GivenIDeleteTheOutputFile()
        {
            string fileName = @”c:PageUpOutexporterTest..txt”;
            File.Delete(fileName);
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = “C:\exporter_delete.bat”;
            p.Start();
        }

The batch file calls a text file which WinSCP needs:

@ECHO OFF
cd c:Program Files (x86)WinSCP
start WinSCP.exe /console /script=c:exporter_delete.txt
:END

The text file consists of more MS DOS commands to perform the operation on the FTP server:

# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect using a password
# open sftp://user:password@example.com -hostkey=”ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx”
# Connect
open sftp://tuneup:iScWxPjvi7Oal6m@securestorage.pageuppeople.com -hostkey=”ssh-dss 1024 9e:bd:6f:c8:f9:68:18:81:f0:50:ce:71:dd:d6:53:2e”
cd /
# Download file to the local directory d:
rm *.txt
# Disconnect
close
# Exit WinSCP
exit

Taking Screen Shots & Image comparison

Selenium offers the ability to take a screen shot of the state of the running test. It enables you to compare the screen of what you have just ran with the one previously ran.

The code that captures the screen shot looks like this:

WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot1 = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);

Image comparison compares 2 image’s:

  • Number of bands, width, and height
  • Pixels by pixels
In my iPhone application tests, I have used this mechanism. The code can be found in code

 

Selenium-Ruby: How To Attach Document On Computer

I encounter a scenario where the browser opens up a window on the computer to attach a file.  I am delighted to find the “attach_file” function.

Below is the code:

Then /^I attach file “([^”]*)” into “([^”]*)”$/ do |filename, id|
filepath=File.expand_path(Dir.pwd)+”/features/support/”+filename
attach_file(id,filepath)
find(:xpath,”//button[@id=’file-submit-button’]”).click
sleep 5
end

IE Does Not Recognise Complex XPath

You may need different code for FireFox and IE. This is because IE does not recognise complex XPath expressions.

For eg:

This will work in FF

driver.FindElement(By.XPath(“/html/body[@id=’ctl00__objBodyTag’]/form[@id=’aspnetForm’]/div[@id=’__Page’]/div[@id=’page’]/div[@id=”]/div/div/div/div[@id=’expandPanelsPersonalDetails’]/div[11]/img[1]”)).Click(); 

In IE it needs to be simplified to:

driver.FindElement(By.XPath(“//div[@id=’expandPanelsPersonalDetails’]/div[11]/img[1]”)).Click();

 

Selenium Grid 2

How To Set Up Selenium Grid 2 For Cross-Browser Testing

This page shows you how to to set up Selenium Grid 2 running in 2 machines, a WINDOWS and MAC, in parallel.
java -jar selenium-server-standalone-2.23.0.jar -role hub
  • Start a node for Firefox in MAC. 10.12.1.9 is the IP address of the Hub machine. I need to specify the host IP, which is the IP of the MAC because I am in a LAN.
java -jar selenium-server-standalone-2.23.0.jar -role node -hub http://10.12.1.9
:4444/grid/register -browser browserName=firefox,version=3,platform=MAC -hubHost 10.12.1.9 -host “10.12.2.18”
  • Start a node for Internet Explorer in WINDOWS (this is where we start up the Hub)
java -jar selenium-server-standalone-2.23.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=”internet explorer,version=3,platform=WINDOWS”
  • If you go to http://localhost:4444/grid/console you should see the following:
 
  • You need to have similar 2 programs running in different browsers
Example:
 
program1.cs runs the test in FireFox
 
private DesiredCapabilities capability = DesiredCapabilities.Firefox();
driver = new RemoteWebDriver(new Uri(“http://localhost:4444/wd/hub”), capability);
driver.Navigate().GoToUrl(“https://admin.pageuppeople.com”);
 
program2.cs runs the test in Internet Explorer
 
private DesiredCapabilities capability = DesiredCapabilities.InternetExplorer();
driver = new RemoteWebDriver(new Uri(“http://localhost:4444/wd/hub”), capability);
driver.Navigate().GoToUrl(“https://admin.pageuppeople.com”);
  • You need to use a test framework that is able to run tests in parallel. I am using MbUnit.
In AssemblyInfo.cs, add the following code to run program1.cs and program2.cs in parallel:
 
[assembly: DegreeOfParallelism(2)]
[assembly: Parallelizable(TestScope.All)]

  • Build the DLL for your tests. I am using Ms Visual Studio
  • Load the DLL via Icarus GUI Test Runner to start running the tests
  • You will have both program1.cs running in MAC using FireFox and program2.cs running in WINDOWS using IE8 in parallel
 
 

Getting Selenium To Work In Internet Explorer 8

Below are the 3 key things which you need to configure in order for Selenium to work in Internet Explorer 8

  • Tools->Internet Options->Security tab
Set the security level for all zones to high. This is due to all zones have to be on the same level and Restricted Sites can only be set to high.
  • Zoom level to be 100%
  • Tools->Internet Options->Security tab->Custom level->Scripting
Enable Active Scripting. This is required in order for JavaScript to work.

  • Install the correct version of InternetExplorerDriver – depending on how many bits is your machine

Integrating Tests Into CI Box

In this example, I am using Cruise Control .NET

  1. Add a project in ccnet.config

The example above do the following:

  • Adds a project in the build that is triggered at 6pm everyday
  • Runs the bat file that runs the tests
  • Copies the output report to be displayed in Dashboard
  • Displays the test report associated with every
  • The “actionName” has to be unique
2. Link the report to Dashboard


 

Automating The Running Of SpecFlow Tests

1. Create a bat file with the following code to kick off a test suite named “Regression” using NUnit:

nunit-console  Regression.dll

2. Add in the following code to use SpecFlow to generate the output in HTML format

specflow.exe nunitexecutionreport Regression.csproj /out:TestResult.html

3. Add the bat file to run as part of the Task Scheduler (Windows 7)

Go to Start->All Programs->Accessories->System Tools->Task Scheduler

4. At the right side, under Actions click on Create Task

5. Under General tab, give a name for the task and select options such as “Run only when user is logged on”

6. Under Triggers tab, click on New, and select the schedule you want the task to run. For example, Daily at 7am.

7. Under Actions, click on New and browse for the bat file

How To Set Up Headless Selenium in C#

 

  1. Install Java
  2. Install Selenium Server
  3. Open Windows command prompt and set Java path:
          set path=C:Program FilesJavajdkbin
 
    4.  Run Selenium Server:
         java -jar selenium-server.jar
 
    5.  Initialise the driver:
 
         IWebDriver driver = new RemoteWebDriver(DesiredCapabilities.HTMLUnit());
 
    6.  Continue with the code as usual:
 
         driver.Navigate().GoToUrl(“http://www.google.com”)

SpecFlow + C# – Read a row from database to check the value

 

 [Then(@”we verify that the (.*) row contains (.*) of (.*)”)]
        public void ThenWeVerifyThatTheFirstRowContainsLInstIDOf410(String row, String field, String value)
        {
            SqlConnection ThisConnection = new SqlConnection(@”Server=1.1.0.26;Database=pp5_410_LK;Trusted_Connection=True”);
            ThisConnection.Open();
            SqlCommand thisCommand = ThisConnection.CreateCommand();
            if (String.Compare(row,”first”)==0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MAX(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            if (String.Compare(row, “last”) == 0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MIN(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            try
            {
                while (thisReader.Read())
                {
                    Console.WriteLine(thisReader[field].ToString());
                    Assert.AreEqual(value, thisReader[field].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            thisReader.Close();
            ThisConnection.Close();
        }
« Older posts Newer posts »

© 2021 Chuan Chuan Law

Theme by Anders NorenUp ↑