Problem from the mock exam:
Here is question 5 from the mock exam V2.0
Open and write TestNG / WebDriver test for the following using Firefox browser: (12 points)
Verify the page heading contains ‘Letterboxd’.
Click on ‘PEOPLE’ from the top menu and capture a screenshot of the resulting page.
Print the names of the reviewers under the ‘Popular this week’ section
In the ‘Popular this week’ section, print the total number of reviews that the first reviewer has.
CP-SAT 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.
Please note that this is a sample solution for question 5 from the V2.0 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 as below:

Project has 2 packages, 1 for test scripts (V2.0) and another one for some helping methods (utility) under test. Create similar package like vapr182 in parallel to write scripts for Mock V2.0 under test source folder. OR, if you want you can create separate project for that.
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 5 – this is in src/test/mock_v2 folder (mock_v2 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 second one for WebDriverWait class. We will be initializing these variables in the @Before method and will use them in the @Test method
As per the question we are using TestNG framework
Here is the script from Selenium/Java

	* Define member variable for WebDriver and WebDriver Wait
	WebDriver driver;
	WebDriverWait wait;
	public void setUp() throws Exception {
		System.out.println("================Execution Started================");
		System.setProperty("", "src\\test\\resources\\drivers\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		wait = new WebDriverWait(driver, 20);

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 website.
Then, defines one  By objects (locators for getting page heading ‘Letterboxd’)
I think you can easily locate the element using link text, one thing to notice here that complete text is ‘Letterboxd — Your life in film’, when you will see in view source you would find the same.
To verify the page heading, we use assert in try catch block, so that it does not impact our test execution if it does not match.
People menu is also a link, all characters of menu are in capital letters on the page but not in view source. But, we have to take the actual one (because we are using linkText locator – so take it as all capital)  that we can see on the page because designer used something to make it all capital.
Here is the script from Selenium/Java

/*      Open         */
		/*      Verify the page heading contains ‘Letterboxd’    */
		By pageHead = By.linkText("Letterboxd — Your life in film");
		WebElement pageHeading = driver.findElement(pageHead);
		try {
		}catch (AssertionError e) {
			// TODO: handle exception
/*		Click on ‘PEOPLE’ from the top menu and capture a screenshot of the resulting page  */
		By link = By.linkText("PEOPLE");
		WebElement linkText = driver.findElement(link);;

Next task is print all the reviewers name from the ‘Popular this week’ section
We can easily locate the element using xpath and it gives all the reviewers name from the section.
This is the xpath of getting reviewers name : (“//*[@id=\”popular-reviewers\”]/ul/li[*]/h3/a
We passed * inside li tag to get all the names and to iterate inside list of elements we used
for loop to get the names one by one.
Last point to print the number of reviewes by first reviewer, we used xpath to get the reviews and it easily returns the numbers.
Here is the script from Selenium/Java

/*		Print the names of the reviewers under the ‘Popular this week’ section      */
		By reviewers = By.xpath("//*[@id=\"popular-reviewers\"]/ul/li[*]/h3/a");
		List reviewersList = driver.findElements(reviewers);
		System.out.println("Reviewers Size : " + reviewersList.size());
		for(int i=0;i< reviewersList.size();i++) {
			System.out.println("Reviewers Name : " + reviewersList.get(i).getText());
/*		In the ‘Popular this week’ section, print the total number of reviews that the first reviewer has.  */
		By totreview = By.xpath("//*[@id=\"popular-reviewers\"]/ul/li[1]/p/a[2]");
		WebElement totalReviews = driver.findElement(totreview);
		System.out.println("Total reviews of first Reviewer : " + totalReviews.getText());

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 Comment