Wednesday, May 11, 2016

Learning Selenium WebDriver

So as you can probably tell my my recent blog posts, I have been trying to get more into coding and learning different things that will help me become a developer in the future.

Since I am a QA Specialist, I've heard the buzzwords "selenium" and "automated test" thrown around the office, but I didn't really know what any of those were. So I decided to start looking into it and try to understand how Selenium works and how to make automated test.

After spending sometime looking at some videos on Pluralsight, I got the basic idea that basically you can use a programming language like C# and write a "script" on what you would like to do in a website. I did some basic examples, but I didn't really think about it until yesterday.

In my current project at work, we are currently working on migrating our existing database from RavenDB to MartenDB. There are many documents that we have saved in the Raven database, and we are going to migrate each type of document. But in order to test it correctly, we were trying to find a simple way that would allows us to generate a lot of activity on the site, like customer's trying to reset passwords and see if it would affect the migration.

I had the idea that I could write a Selenium script that would do this for us on the site. Here is what I came up with:

using System;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest
{
    class GenerateTokens
    {
        public static void Main()
        {
            Generator();
            Environment.Exit(1);
        }

        private static void Generator()
        {
            IWebDriver driver = new ChromeDriver(@"C:\Libraries\");
            driver.Url = "some-site.com";

            Debug.Assert(driver.PageSource.Contains("A message has been sent with directions on how to reset your password.") == false);

            for (int counter = 0; counter < 100; counter++)
            {
                driver.FindElement(By.Id("forgot-link")).Click();

                IWebElement email = driver.FindElement(By.Name("Email"));
                email.SendKeys("some-email@some-domain.com");

                driver.FindElement(By.ClassName("btn-primary")).Click();

                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));

                Debug.Assert(driver.PageSource.Contains("A message has been sent with directions on how to reset your password.") == true);
            }

            driver.Quit();
        }
    }
}

This script would essentially go to a site and go through the steps a customer would take to recover their password on their account. The script would repeat it 100 times (but it can be increased if we wanted) and we wouldn't have to worry about doing it ourselves manually. The downside to this script, is that it would perform the same action for the same account/email address provided. I was thinking of maybe modifying this script to read a text file that contained a lot of different email address that it would just input on the site.

I don't think this script will actually get used at work since I am not a QA Engineer, and I think they might have a better approach at doing this with some internal tool, but I thought that it was interesting how easy this was to write.

Anyways, that's all for now. Thanks for reading!

No comments:

Post a Comment