CPSAT Mock Exam Questions -Solution 2

 

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

————————————————————————————————————————————

Solution:

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.

If you run into any issues you can reach out to us via the CPSAT contact us page.

http://cpsat.agiletestingalliance.org/#contact

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)

  • 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
  • Drivers – Browser drivers (Drivers to communicate with the browser)
  • 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

https://www.dropbox.com/s/wzp8nfjf4nuj3q0/pom.xml?dl=0

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)

https://www.dropbox.com/s/qeoeqmc550k6n8i/Que2_Wikipedia.java?dl=0

 

File 2 – Java Code for the helper functions used in the above question

https://www.dropbox.com/s/oijwpwh8hg6653q/HelperFunctions.java?dl=0

 

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

/*

* Define member variable for WebDriver and WebDriver Wait

*/

WebDriver driver;

WebDriverWait wait;

 

@BeforeTest

public void beforeTest() {

 

System.out.println(“================Execution Started================”);

System.setProperty(“webdriver.chrome.driver”, “src\\test\\resources\\drivers\\chromedriver.exe”);

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

wait = new WebDriverWait(driver, 10);

}

 

===========================================

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

// CP-SAT Mock Solution for the reference

@Test

public void test() {

 

driver.get(“http://www.wikipedia.org”);

By englishArticlesBy = By.xpath(“//*[@id=\”js-link-box-en\”]/small/bdi”);

String finalEnglsihArt = driver.findElement(englishArticlesBy).getText();

System.out.println(“No of Articles in English :”+finalEnglsihArt);

By englishLink = By.id(“js-link-box-en”);

WebElement weenglishLink = driver.findElement(englishLink);

weenglishLink.click();

 

// Waiting for next page to load otherwise it start entering the text on this page search box

// because of the attribute based locators are same

wait.until(ExpectedConditions.titleContains(“Wikipedia, the free encyclopedia”));

By searchBox =By.id(“searchInput”);

 

// Clear the field first before enter as it is a best practice

driver.findElement(searchBox).sendKeys(Keys.CLEAR);

driver.findElement(searchBox).sendKeys(“Anna University”);

driver.findElement(searchBox).sendKeys(Keys.ENTER);

 

======================================

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

By mottoinEnglish = By.xpath(“//*/table[@class=’infobox vcard’]/tbody/tr[2]/td”);

String  motto = driver.findElement(mottoinEnglish).getText().trim();

System.out.println(“Moto in English : ” + motto);

 

// Checking the motto has word knowledge in it means partial check

if(motto.contains(“Knowledge”)) {

System.out.println(“Knowledge is present in Motto : ” + motto);

}else {

System.out.println(“Knowledge is not present in Motto : “+motto);

}

 

// Fetching list then iterating loop to check the ‘Shiv Nadar‘ is listed or not in the same

By byListNotable = By.xpath(“//*[@id=’mw-content-text’]/div/div[4]/ul/li[*]”);

List <WebElement> listNotablePeople  = driver.findElements(byListNotable);

int size = listNotablePeople .size();

for(int i=0; i<size; i++) {

WebElement weindex = listNotablePeople .get(i);

String strnotableP = weindex.getText().trim();

 

// ensuring the name using conditional statement

if(strnotableP.contains(“Shiv Nadar”)) {

System.out.println(“Shiv Nadar is listed in list of notable people in the “+i+ ” th position”);

break;

}else {

System.out.println(“Ship Nadar not found in ” +i+”th position in the list”);

}

}

 

====================================

Hope this was helpful.

If you do not understand something or have doubts please get in touch with us using the CPSAT contact 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

Leave a Reply