Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, March 4, 2020

Convert/Export Recorded Web, Android & iOS Tests to Java & C# with TestProject


Prerequisites:
1. Sign up: Open a browser  -> Visit TestProject and sign up
2. Activate your TestProject A/C: After signup go to your email and activate account by clicking 
    the link provided through your email
3. Login: Go to TestProject -> Click Login link at the top right corner and log in
4. Get TestProject Agent: Mouse over Agents menu item and download agent for your preferred
    OS (Windows, Linux, Mac)
5. Install, Launch and Register Agent: Install & launch Agent -> Register it
6. Record test: Record your desired tests by following the instructions from the link below:

The following is my recorded tests of Google search:

Now is the time to export your Recorded Tests to Code (Java or .NET C#) ...

Export recorded test to Java or .NET C#:

1. In order to export recorded tests to code, 1st step is to click the Three Dots at the right corner of
    your test and choose to “Generated Code” for the test you want to export as the screenshot as
    below:

2. The following option will be displayed:

3. Select Java or C# as programming language and select your preferred browser (such as Chrome,
    Edge, Firefox, IE, Safari) -> Click Generate button as shown in the screenshot above.
4. Save As option will be appeared for saving the exported code as below screenshot:

5. Now select your desired location -> Click Save button as above screenshot. Click OK from
    Generate code pop up.
6. Extract the zip file you saved (Say, Web_Test_2-My_Test_2-2020_03_02_12_25.zip).
7. Open the extracted code in your preferred IDE. Such as
    for Java you can use Eclipse, IntelliJIDEA.
    for .Net C# you can use Microsoft Visual Studio.


So, You can try TestProject for easier recording your tests as well as to convert your preferred programming language for making flexible & customizable.

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