Saturday, 13 July 2019

Automating email validations through Selenium and disposable email service

There are some aplications which sends Welcome emails/User registration emails to activate accounts etc. Also there are some systems which sends marketing emails. For testing them, manually verifying these emails is always tedious and time consuming. What if we have a way to automate them just through Selenium. Sounds Interesting? Lets see how it works.

We all know that Selenium can only interact with HTML elements. So, to verify the mails, we need to open our email in browser. Luckily we have a free disposable email service which opens up our mail in a browser called Endtest Mailbox. All we need to do is, use any mailid with the extension '@endtest.io'.

The username can be anything you choose.
For instance, you can choose something like ram12234@endtest.io.

You can access the Inbox for that email address in browser just by adding the email parameter in the URL, like this:
https://endtest.io/mailbox?email=ram12234@endtest.io

Please note that these emails will be deleted after 2 hours.

To demonstarte this I am using thissite . When you enter an email id and click on check, a test email is sent to the given email id immediately. Lets consider this as our application and we need to verify if it sends an email correctly to the given email address!

driver.get("http://ismyemailworking.com");
driver.manage().window().maximize();
driver.findElement(By.id("verify_email")).sendKeys("ram12234@endtest.io");
driver.findElement(By.id("content_cob_check")).click();

The above code will send a test mail to your inbox. Now we can open the inbox in browser through selenium with the link https://endtest.io/mailbox?email=ram12234@endtest.io.



driver.get("https://endtest.io/mailbox?email=klaus123@endtest.io");
driver.findElement(By.id("email_item")).click();

After this, we can add all the assertions such as Subject,  email body etc.

There are also few alternatives for Endtest mailbox such as mailnatorguerrillamailNada

Note:
If your emails are confidential, this solution is not appropriate. Because these free mailboxes are public and can be accessed by anybody.

In our next post, we will discuss about 'how we to verify the mails received in your Gmail Account or outlook'. 

Tuesday, 9 July 2019

Fake it until you make it - Stubby4j

While I was searching for a Contract Testing tool, I came across this interesting Stubby4j. Stubby4j is highly flexible and configurable tool for service virtualization or mock API server. It is actually a HTTP server that uses embedded Jetty which allows stubbing of external systems with ease for integration, contract & behavior testing.

In today's microservices era, it is very common that an API you depend on doesn't exist or isn't complete.  To stay productive in those situations, we can use these mock API servers which constantly produces the desired response. We can also use them while testing edge cases as the real APIs don't reliably produce these scenarios.

Key Features of Stubby4j:

  • Emulate external webservice in a SANDBOX for your application to consume over HTTP(S)
  • HTTP request verification and HTTP response stubbing
  • Regex support for dynamic matching on URI, query params, headers, POST payload (ie:. mod_rewrite in Apache)
  • Dynamic token replacement in stubbed response, by leveraging regex capturing groups as token values during HTTP request verification
  • Record & Replay. The HTTP response is recorded on the first call, having the subsequent calls play back the recorded HTTP response, without actually connecting to the external server
  • Dynamic flows. Multiple stubbed responses on the same stubbed URI to test multiple application flows
  • Fault injection, where after X good responses on the same URI you get a bad one
  • Serve binary files as stubbed response content (images, PDFs. etc.)
  • Embed stubby4j to create a web service SANDBOX for your integration test suite

Quick Start:

1. Download the latest stubby4j version (the JAR archive).

2. Create the following local YAML file:
 -  request:
      method: GET
      url: /hello-world
 
   response:
      status: 200
      headers:
         content-type: application/json
      body: Hello World!
3. Execute the downloaded stubby JAR using command
java -jar stubby4j-x.x.xx.jar -d <PATH_TO_YOUR_CREATED_LOCAL_YAML_FILE>
4. Navigate to http://localhost:8882/hello-world to get the stubbed response “Hello World!”

5. Navigate to stubby4j admin portal at http://localhost:8889/status to see what has been stubbed & other useful data

Apart from using it is as a jar file, we can also use Stubby4j as a docker container.

Running Stubby4j as Docker Container:

Dockerfile example:
FROM <any-image-that-includes-java>
RUN wget http://central.maven.org/maven2/io/github/azagniotov/stubby4j/6.0.1/stubby4j-6.0.1.jar -O /usr/local/stubby4j.jar
EXPOSE 8889 8882
CMD java -jar /usr/local/stubby4j.jar -d no.yaml --location "0.0.0.0" --watch 
By default http://localhost:8882 is the stubs portal, http://localhost:8889 is the admin portal. They should be exposed externally.

The command for running a docker image
docker run -p 127.0.0.1:8889:8889 127.0.0.1:8882:8882 <your-image>

How QE can use Stubby4j:

  • Specifiable mock responses to simulate page conditions without real data.
  • Ability to test polling mechanisms by stubbing a sequence of responses for the same URI
  • Easily swappable data config files to run different data sets and responses.
  • All-in-one stub server to handle mock data with less need to upkeep code for test generation

There are several tools available for Service Virtualization or mock API server. But Stubby4j has the following advantages over other tools.
  1. You can simulate responses from real servers
  2. Simulates support for different types of HTTP authentication.
  3. Enables delayed responses for performance and stability testing
  4. Allows invocation of service call for services which are not ready yet or in-accessible due to various reasons.
Apart from it's advantages, stubby4j also has below limitations.
  1. You can neither change the name of the mapping file nor break it in multiple files.
  2. Data that you capture from request using regular expression can be used either in response mapping or response body (if response body is specified in separate file)
  3. There is no way to generate dynamic dates, random numbers etc. The data which is captured from the request can only be used in response.
  4. If your simulator application grows, .yaml is not maintainable
There is a tool called Stubmatic which adresses many of these limitations. 

For more information on stubby4j, please visit https://github.com/azagniotov/stubby4j

You can also look at below alternatives for Stubby4j:

Monday, 4 February 2019

Zalenium – A Dockerized Selenium Grid

Over the years Selenium users have used Selenium Grid extensively to reduce the execution time, to perform cross browser testing, to perform multiple OS testing. Although grid provides us all these benefits, they come with their own cost. If you look at the issues that we are facing now with grid are:

                           
  • Have a stable grid to run UI tests with Selenium
  • Maintain it over time (keep up with new browser, Selenium and drivers versions)
  • For longer executions Java process will run out of memory.
  • Restarting the nodes in case of issues
  • Scaling up for more tests is tedious.

Zalenium provides the solution for above problems. It is a docker-selenium based solution which can start a Selenium Grid in seconds, a grid that scales up and down dynamically to run your tests in Firefox and Chrome. It works out of the box in docker and Kubernetes. Zalenium also provides the video recordings of your tests and live preview of your test execution.

Zalenium Setup:


1. Make sure that the latest Docker is up and running. (check here for instructions to install  and run)

2. Open command prompt/ terminal and run the below commands to pull the Docker images from Docker hub.
  • docker pull elgalu/selenium
  • docker pull dosel/zalenium

3. start the Zalenium-selenium-grid by executing the below command
  •     Mac:
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 \  
 -v /var/run/docker.sock:/var/run/docker.sock \  
 -v /tmp/videos:/home/seluser/videos \  
 dosel/zalenium start  
  •      Windows:
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/videos:/home/seluser/videos --privileged dosel/zalenium start  

4. Zalenium grid is ready now for the execution. Initially zalenium creates one chrome and one firefox container. If you need more containers, we have to specify them as parameters like below while launching our grid.

 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/videos:/home/seluser/videos --privileged dosel/zalenium start --desiredContainers 4  

5. Now write a selenium test using remote webdriver. Look at the below script for reference.

 @Test  
 public void zaleniumDemoTest()  
 {  
 DesiredCapabilities capability = DesiredCapabilities.chrome();  
 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);  
 driver.get("https://<www.yoursite.com>/");  
 driver.manage().window().maximize();  
 Thread.sleep(3000);  
 driver.quit();  
 }  

In above script, we need to specify the IP and port address of the machine where Zalenium is running as URL.

6. If you run the test now, it will execute in zalenium-grid.

Live Preview:

As discussed above, Zalenium provides live preview of your execution.  You can watch your test execution live once your tests are triggered. You can access the live preview from the below link:

To Auto-refresh, add ?refresh=numberOfSeconds to refresh the view automatically.

E.g. http://localhost:4444/grid/admin/live?refresh=20 will refresh the page every 20 seconds.

DashBoard:

  • Zalenium also provides very useful dashboard for monitoring the test results. From this dashboard you can replay those recorded videos. Below is the link for the dashboard.

              http://localhost:4444/dashboard/#

         

  • You can replace localhost for the IP/machine name where Zalenium is running
  • Click on Cleanup to remove all videos and logs from the local drive and the dashboard.Also reset the dashboard via http://localhost:4444/dashboard/cleanup?action=doReset or cleanup via http://localhost:4444/dashboard/cleanup?action=doCleanup

To stop the docker containers spun through Zalenium, simply exit Zalenium grid using the below command

 docker stop zalenium.  

Integrating with Cloud Solutions:

Zalenium usually creates containers only with Chrome and firefox. If you want to execute your tests in any other browser or any other platform, you can still use other cloud based platforms such as Saucelabs, Browser stack. Zalenium enables you to integrate these platforms with the existing zalenium-grid.

Below command helps to integrate with Saucelabs:
 export SAUCE_USERNAME=[your Sauce Labs username]  
 export SAUCE_ACCESS_KEY=[your Sauce Labs access key]  
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 \  
     -e SAUCE_USERNAME -e SAUCE_ACCESS_KEY \  
     -v /tmp/videos:/home/seluser/videos \  
     -v /var/run/docker.sock:/var/run/docker.sock \  
     dosel/zalenium start --sauceLabsEnabled true  

You can visit Zalenium Site for more details.


Tuesday, 25 December 2018

New Selenium IDE supports Chrome & more features


       Selenium Integrated Development Environment best known as Selenium IDE, is the simplest and easiest tool in whole Selenium suite to install and get introduce to selenium environment for the beginners. This was initially released as a Firefox add-on that creates tests quickly through record-and-playback functionality. This was even being used by the Selenium advanced users to identify the objects on a page using locator techniques such as xpath, css, id etc. After the recent changes in Firefox browser, Selenium IDE has stopped working for a while now.

But Selenium team has just released new Selenium IDE. 

Let’s discuss the new features of latest Selenium IDE in this post.

    1. Built as Web Extension & Chrome Support:
The latest Selenium IDE is built as a web extension. Because of this, now we have  Selenium IDE support for chrome browser as well. The Sideex open source project was used as a base to build this IDE.
    Installation in chrome:
a.   Open this in your chrome browser and click on “Add to Chrome”


b.   Once you add it, you can seeicon on top of your browser, beside the address bar.
c.   If you click on the icon, Selenium IDE would open up as a popup,

d.   You can start using it, by clicking on “Record a new test in a project” and naming it.
    Installation in firefox:
a.   Open this in your firefox browser and click on “Add to Firefox”
b.   Once you add it, you can see  icon on top of your browser, beside the address bar.
          c. Other steps are same as above.

2. Improved locators:
 Now selenium IDE supports auto populated, improved, multiple fallback locators. Once you record any test, select a step where you want to change the object locator. Then click on the “Target” drop down. You can see multiple options like css, xpaths, name, id etc.

     3. Conditional & Control flows:
The latest Selenium IDE supports Conditional flows by using if, elseif, repeatif commands. With these commands, we can specify the conditions to make our recorded script more intelligent.
                        
IDE also supports control flow using while command. A condition can be specified to execute a specific step repeatedly.

4. In-built implicit wait:
Selenium IDE will now automatically wait for every command, every command will wait for the page to load. Commands which take a locator will wait for the element to appear. This way we no need to use explicit waits.

5. Adding assertion and verifications while recording itself:
Now users can add assertions and verifications in tests by using the context menu on the page. Once the page gets launched from IDE, select the element that needs to add as assertion. Now right click on that element. You can see a context menu, with multiple options.

    
6. Running in CI:
We can execute the selenium IDE scripts in CI now. If you click on 3 dots next to save icon in selenium IDE, you can see an option called “Running in CI”. Clicking on this option takes you to here. This page has very clear explanation on how to execute these .side scripts though CI. We can also use selenium server to run the tests in remote machines.


According to Webdriver creator Simon Stewart, below features are in pipeline for Selenium IDE team.

1. Exporting Selenium IDE Scripts to Programming languages (Java/JUnit/Python/PHP):
    Exporting the .side Scripts to programming languages is not supported yet in the latest IDE. But this will be supported in future starting with Java.

2. Data driven Testing:
    Even the old IDE was not supporting data driven testing. But it is being considered in the latest IDE. We may see this feature as well in future.

Thursday, 6 December 2018

Useful tricks and tips for working with POSTMAN


      Postman is a powerful graphical HTTP client helpful when working with APIs. In our day to day work, we use postman for our REST Service tests a lot. In this post I am going to discuss about few tips and tricks while using postman.

1. Variables:
    We can use variables to replace certain fields such as the URL, URL parameters, headers, authorization, request body, and header presets etc. in postman. These variable names are enclosed with double curly braces – like {{variable name}}. Postman uses string substitution to replace the current value of this variable.


There are multiple ways to initialize, retrieve, and define the scope of these variables. Below is the practical usage of this feature.

How I used variables:
    The most annoying problem we face in our everyday usage of postman is, updating the Auth token. As the auth token has limited validity, we have to replace the token every time when it gets expired. This hack helps the users to include this token generation in the run time, so that we don't need to update every time.

    First, we need to add the below script under "Pre-request Script" tab. (The Pre-request Script is a tab where one can write javascript code to hit any endpoint and extract data from a response to use this data in a subsequent request. So, if you include any script in this tab, this will be executed even before making your REST Call. Thus it initializes & loads the variable even before making your actual Call.)


const echoPostRequest = {
  url: 'http://ur.access.token.endpoint',
  method: '<GET or POST>',
  header: 'Content-Type:application/json'
};

var getToken = true;
console.log(pm.sendRequest);

if (getToken === true) {
    pm.sendRequest(echoPostRequest, function (err, res) {
        console.log(err ? err : res.json());
        if (!err) {
            pm.globals.set("token", res.json()["<field name of the response where your token is stored>"]);
        }
    });
}


In the above script, we are storing the token value in the variable called “token”. Now we use this variable in the header as the value of “Authorization” like below (You may be using different key for authorization. Use the same key here).


That's it. Now you can run this collection, whenever you want without worrying about replacing latest token.

2. Dynamic Variables:
     Postman has a few dynamic variables you can use in your requests. We can use them in the request builder as unique IDs, timestamps, or random integers. The syntax will be {{$timestamp}}. Below are those variables and their purposes.

{{$guid}} : Adds a v4 style guid
{{$timestamp}} : Adds the current timestamp
{{$randomInt}} : Adds a random integer between 0 and 1000


3. Data Variables:
   Data variables are available when using a data file with the collection runner. You can import the data as a CSV or JSON file, and then use the values from the data file inside HTTP requests and scripts. Here's an example of Inside pre-request and test scripts. Let's say you have the pm.iterationData.get("username") method inside pre-request and test scripts. The method would let you access the value of the username variable from a data file.


You can get more details on how to use data variables from the below link


Friday, 6 April 2018

Webservices Automation Framework

Today, a faster test cycle at lower cost is vital to stay competitive. Re-usable test automation frameworks coupled with open source tools and technologies is a key solution to shrink test cycle time and related costs. Here I have come up with a very robust Automation framework for automating RESTful webservices. I used Rest-Assured to build this framework. Below is the link to it:  https://github.com/nainappa/ConfianceAPIAutomationFramework

Thursday, 22 March 2018

Replace Jenkins Logo and Text of your choice in JenkinsUI

Often times we have several Jenkins CI Servers and we always get confused to determine them. Some times we may need to change the Logo and text to customize Jenkins UI to reflect your organization details. This post helps to you to change the header and Logo on your Jenkins UI.

Custom styling can be applied via modification of the CSS structure being used by Jenkins.
Simple Theme Plugin is the most popular approach to customizing CSS outputs.

Below are the steps to customize your Logo and Header Text:
  • Install Simple Theme Plugin
  • Go to the <JENKINS_HOME>/userContent directory in your jenkins host machine.
  • Create the layout directory
  • Create the style.css file, copy the contents provided below.
  • Customize the “Sample Project” text
  • Put the logo of your instance to <JENKINS_HOME>/userContent/layout/logo.png. The logo should have 40px height.
  • Go to the global configuration of your CJP instance and find the Theme section there (Manage Jenkins--> Configure System)
  • In URL of Theme CSS specify the following path: <JENKINS_HOME>/userContent/layout/logo.png eg:http://localhost:8080/userContent/layout/logo.png

Below is the css code to be used. Make sure you make the required changes as mentioned above.

/* Custom style for Autobahn Jenkins Platform */
.logo img {
  content:url("<JENKINS_HOME>/userContent/layout/logo.jpg");
  /*change the path to your logo path*/
}

#jenkins-name-icon {
  display: none;
}


.logo:after {
content: 'Sample Project';
font-weight: bold;
font-size: 40px;
font-family:"Brush Script MT", cursive;
margin-left: 200px;
margin-right: 12px;
color: Aqua;
line-height: 40px;
}