Problem from the mock exam:
Here is question 2 from the VAPR182 mock exam
Open http://www.wikipedia.org and write JUnit/WebDriver test for the following using Google Chrome:
Display on console the number of articles in English and click on “English” link
Search for Anna University.
Print the ‘Motto in English’ part in the console and ensure it has the word ‘Knowledge’ in it.
Ensure that “Shiv Nadar” is listed in notable people section
CPSAT believes in practice, and the mock exams are given to prepare you for the exam. If you have not attempted the question on your own, we do recommend that you try to solve the question first before reading any further.
Please note that this is a sample solution for question 2 from the VAPR182 mock exam
* Please note that this may not be the only solution, there could be potentially other ways to solve the problem, specially the way the locators can be handled, data can be handled etc. The intent here is to help you with a sample solution code.
* This is for reference purposes
The question is solved using Java and Maven Project. Here are some details
Maven Project Structure is shown in image below:
Project has 2 packages, 1 for test scripts (vapr182) and another one for some helping methods (utility) under test.
And, there are 3 folder for the resources that will use in scripts (we will provide the relative path of the resources in the Java code)
1. Data – Excel sheet (we need to read data from .xls or .xlsx files – we will keep the data files here) – Not required in this question
2. Drivers – Browser drivers (Drivers to communicate with the browser)
3. Screenshots – If needed during the test scripts
As we know maven is a build tool, if need external librareries in the maven project we define dependency in the pom.xml file and it automatically fetch the libaries from the maven central repository and it keeps in the local repository of the machine.
Here is the URL for the POM.xml file that we have used
Before we start explaining the code and if you want to get access to the data file and the java code, you can download the 3 files we are going to use from the the following URL’s
File 1 – Java Code for the Question 2 – this is in src/test/vapr182 folder (vapr182 is the package name thus)
File 2 – Java Code for the helper functions used in the above question
Code explanation is given below
Part 1 of the code is as below
We have two member variables, one for WebDriver and another for WebDriverWait class. We will be initializing this in the @Before method and using them in the @Test method
As per the question we are using Junit framework
=========================================== Part 1 of code
Part 2 of the code is as below
The 2rd part of the code is the starting of the @Test .
The below code begins with opening the wikipedia.com website and print the number of articles that available in english, then clicking on the English link .
It then defines two By objects (locators for no. of english articles and english link elements on the page)
The challenge in the exercise is that attributes of the search box are same on first page (Main Page) and second page (The main target search box – after clicking on English link it redirects on that).
Please see the below image for the reference
So, when we click on the english link it takes some time to load the next page, simultaneously my next test script start executing, however as told earlier the attributes are same so it starts entering value in the search box of the first page not on the second page (next page)
To handle this problem using Web driver wait, in which checking the expeted condition as title of the next page. So it will be waiting to get focus on the next page.
========================================= Part 2 of Code
Part 3 of the code is as below
This is the remaining part of the test method.
Contains is a method to check the partial text inside the string. So for checking ‘Knowledge’ in the motto, used the same.
The positions of the people in the notable pepeole section may change later. So directly looking for the element of ‘Shiv Nadar’ is not the correct way as script may fail soon.
For Example : If shiv Nadar is moved from 10th position to 1st poition or may be to 40th position.
The best way to check, get the list of notable people and check one be one ‘Shiv Nadar’ is listed or not in the same.
==================================== Part 3 of code
Hope this was helpful.
If you do not understand something or have doubts please get in touch with us using the CPSAT Contact Us Page.
In case you have better/elegant solution you can also share with us. We will love to share the same on this page.
Please note that CPSAT is the number 1 certification in Selenium as it is the only globally recognized certificate which assess practical knowledge by posing similar questions.
CPSAT is now the only globally recognized certificate which not only assesses practical knowledge, but the knowledge can be assessed in three different streams. Java, C# and Python
Happy learning and happy test automation