Wednesday, November 22, 2017

How to set up Selenium WebDriver .NET C# project with NUnit in Visual Studio

Prerequisite:

➽ Windows as OS
➽ Visual Studio is installed

Step 1: Creating Solution and Project in VS

1. Open Visual Studio
2. Click File -> Mouse over New -> Click Project...
3. Select Templates -> Visual C# -> Test at the left side
4. Enter a Solution and Project Name -> Click OK


Step 2: Adding/Installing Selenium WebDriver package to the project

Installing Selenium.WebDriver and Selenium.Support
1. Right click on the Project -> Click "Manage NuGet Packages..."
2. In the middle panel: Click Browse -> Do search by Selenium
3. Click "Selenium.WebDriver" -> Click Install button -> Follow the installation instruction
4. After successful installation - click "Selenium.Support" -> Click Install button -> Follow the installation instruction

Step 3: Adding/Installing NUnit package to the project

Installing NUnit and NUnit3TestAdapter
1. Search by NUnit
2. Click NUnit from the list after search -> Click Install button -> Follow the installation instruction
3. After successful installation of NUnit - click NUnit3TestAdapter -> Click Install button -> Follow the installation instruction

Step 4: Creating a Test

1. In Solution Explorer at the right side - Double click on UnitTest1.cs -> Code snippet will display in the middle panel
2. Rename UnitTest1.cs as MyFirstSeleniumTest.cs at the right panel
3. Replace the existing code by your own.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace MyFirstSeleniumTest
{
    [TestFixture]
    public class MyFirstSeleniumTest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "https://link.testproject.io/bsg";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheUntitledTest()
        {
            driver.Navigate().GoToUrl(baseURL);
            driver.FindElement(By.LinkText("Free Sign Up")).Click();
            driver.FindElement(By.Id("first-name")).Click();
            driver.FindElement(By.Id("first-name")).Clear();
            driver.FindElement(By.Id("first-name")).SendKeys("Ripon Al Wasim");
            driver.FindElement(By.Id("email")).Clear();
            driver.FindElement(By.Id("email")).SendKeys("riponalwasim@gmail.com");
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

Step 5: Running the Test

1. Right click anywhere at the code editor
2. Click Run Tests from the pop up

Friday, November 17, 2017

How to set up Selenium WebDriver Java project in Eclipse

To set up Selenium WebDriver Java project in Eclipse

Prerequisite:

➽ Eclipse is installed
➽ JDK is installed

Step 1: Creating Java Project in Eclipse

1. Open Eclipse
2. Click File -> Mouse over New -> Click Java Project
3. Enter a Project Name -> Click Next and then Finish

Step 2: Installing/Configuring Selenium WebDriver

1. Download Selenium WD Java client from http://www.seleniumhq.org/download/ at "Selenium Client & WebDriver Language Bindings" section (In my case it is selenium-java-3.7.1.zip)
2. Extract the Zip file in a folder/location of your choice
3. In Eclipse: Right click on Java Project you have created in Step 1 and click Properties
4. In Project Properties window: Click "Java Build Path" at the left -> Click Libraries tab -> Click "Add External JARs..." button at the right side
5. Select client-combined-3.7.1.jar and client-combined-3.7.1-sources.jar from the folder "selenium-java-3.7.1" -> Click Open button
6. Again click "Add External JARs..." button at the right side -> Select all the JARs from /selenium-java-3.7.1/libs folder and click Open button
7. Finally click OK

Step 3: Creating our first Test Script/Class

1. To create a package: Right click src -> Mouse over New -> Click Package. Enter a package Name and click Finish
2. To create a class: Right click on package name -> Mouse over New -> Click Class. Enter a Class Name and click Finish button

Step 4: Modifying the previous code to WD java

1. Modify the previous code to Selenium WD Java code to perform the following steps:
    1.1 Initialize FirefoxDriver
    1.2 Visit Google - https://www.google.com/ in Firefox
    1.3 Clear the Google search text field
    1.4 Enter search keyword in text field
    1.5 Press Enter key to view the search result
Step 5: Run the test

1. Right click on Java Class (GoogleSearch.java) at the left
2. Mouse over Run As -> Click Java Application

Tuesday, November 14, 2017

How to get the number of tabs opened in a window by Selenium

Selenium WebDriver Java code:

Scenario:

1. Go to Home page
2. Click on a link to open new tab/window
3. Open more tab/window if it needs
You have more than one tabs. You can count the number of tabs/windows opened in a browser by the following code snippet:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
System.out.println("No. of tabs: " + tabs.size());

Monday, November 13, 2017

Running Selenium Test in Microsoft Edge

Prerequisite: Windows 10 is installed on your machine
  1. Download the specified Microsoft WebDriver server version for your build (In my case it is MicrosoftWebDriver.exe for the Operating System: Windows 10 Pro 64-bit (10.0, Build 14393))
  2. Selenium WebDriver Java code for MS Edge is as follows:
    System.setProperty("webdriver.edge.driver", "D:\Ripon\MicrosoftWebDriver.exe");
    driver = new EdgeDriver();

Friday, November 10, 2017

Running Selenium WebDriver python bindings in chrome

There are 2 ways to run Selenium python tests in Google Chrome. I'm considering Windows (Windows 10 in my case):

Prerequisite: Download the latest Chrome Driver from: https://sites.google.com/a/chromium.org/chromedriver/downloads

Way 1:

i) Extract the downloaded zip file in a directory/location of your choice
ii) Set the executable path in your code as below:

self.driver = webdriver.Chrome(executable_path='D:\Selenium_RiponAlWasim\Drivers\chromedriver_win32\chromedriver.exe')

Way 2:

i) Simply paste the chromedriver.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:

self.driver = webdriver.Chrome()

Thursday, November 9, 2017

How to convert Selenium IDE tests into Java and other languages

By default, Selenium IDE tests are recorded as HTML format. This HTML test script can be converted to Selenium WebDriver test script in various supported programming languages such as Java, C#, Ruby, Python, Perl, PHP etc.

There are 2 ways to convert a recorded tests in Selenium IDE into Java code:

1st way: To see the Java code directly in Selenium IDE

1. Open Selenium IDE -> Record a test
2. Go to Options menu -> Options...
3. At "Selenium IDE Options" window: Check the check box for "Enable experimental features" and click OK button
4. Options -> Format -> Java / TestNG / WebDriver
5. Click OK button from "JavaScript Application" pop up -> You will see the desired Java source code for your recorded tests

2nd way: By exporting recorded test case(HTML format) into java file:

1. Record your tests in HTML format by using Selenium IDE
2. File menu -> Export Test Case As -> Java / TestNG / WebDriver
3. Save the file (it would be saved as .java)
4. Open the java file you saved in any text editor or IDE such as, Notepad++, Notepad, Wordpad, Eclipse, IntelliJ IDEA etc. (You can see the expected Java Selenium source code)

There is another way to convert Selenium IDE command individually to any language. The following steps for java:

1. Open Firefox -> Open Selenium IDE
2. Record some steps of your test
3. In Selenium IDE: Go to Options | Clipboard Format and select Java / TestNG / WebDriver
4. Right click any command in Selenium IDE you recorded -> Click Copy
5. Paste in any editor (such as NotePad, Wordpad etc. OR any IDE you are using such as eclipse, IntelliJ IDEA)
6. The command will be pasted as Java format

Note: In similar way you can convert individual Selenium IDE command to C#, Python and many more you want.