CP-SAT MOCK QUESTION SOLUTIONS – 5

 

Problem from the mock exam:

Here is question 5 from the VAPR182 mock exam

……………………………………………………………………………………………………………………………………………………….

Open the link http://mu.ac.in/portal and write JUnit / WebDriver test for the following using Chrome

  1. Under “Faculties”, select “Science”, and then select “Department of Information Technology”.
  2. Print the Text Present under “Vision” in the console
  3. Click on “Contact Us” and print the Address in the console.
  4. Verify that the phone number “022-26500208” is present.
  5. Verify that the email id uditoffice@gmail.com is present
  6. Save the screen shot image at this test execution instant

……………………………………………………………………………………………………………………………………………………….

Solution:

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.

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

Please note that this is a sample solution for question 5 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 as 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 3 – this is in src/test/vapr182 folder (vapr182 is the package name thus)

https://www.dropbox.com/s/yg4vg2gd6j2hrkd/Que4_ManipalUniversity.java?dl=0

 

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

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

 

Code explanation is given below

Part 1 of the code is as below

———————–

We have three member variables, one for WebDriver, second one for baseURL and third 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 Junit framework

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

Part 1 of code

static WebDriver driver;

                static String baseurl;

                WebDriverWait wait ;

                @Before

                public void startDriver() throws Exception{

 

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

                                driver = new ChromeDriver();

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

                                baseurl=”http://mu.ac.in/portal”;

                                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 https://mu.ac.in/ website.

Then, defines one  By objects (locators for hovering on Faculties Menu)

I think you can easily locate the element using xpath.

But for Science element, please notice one  point here, some web elements only visible after some action, that means the elements are on the page but they are not availble for any kind of action. So if we do some action at this point, there are chances we may get ElementNotFound exception.

So what we will do here first will hover on the element Faculty that is visible on the page then will check the visibility of the science element with the help of explicit wait.

For hovering, Action class is available in selenium that is the only preferred way to handle hovering elements. However, sometimes direct xpath may work but not the recommended way.

So now science element is availble for the action, so just click on the element.

Now next element ‘Department of Information Technology’ is simply available on the page. We can locate the element with the help of linkText or partialLinkText locator.

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

 

Part 2 of code

driver.get(baseurl);

// Create object and wait until the target element visibility is clear

By byMenu = By.xpath(“//*[@id=’menu-item-3694′]”);

WebElement element = driver.findElement(byMenu);

Actions builder = new Actions(driver);

builder.moveToElement(element).build().perform();

wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(“//*[@id=’menu-item-3821′]”)));

driver.findElement(By.xpath(“//*[@id=’menu-item-3821′]”)).click();

driver.findElement(By.linkText(“Department of Information Technology”)).click();

 

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

Next task is print the Text Present under “Vision” in the console.

Please note down one point here earlier there was no pagination and ‘Vision’ was on the current page but now it’s in subsequent page, hence if we run our old test, script may fail here.

So what we will do here, we will write our script in such a way that if in future designer changes the location of the vision text again, suppose on the first page or on any other subsequent page but it should not impact our test script.

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

 

Part 2 of code

List<WebElement> abc = driver.findElements(By.xpath(“//*[@id=\”content\”]/div/div[7]/ul/li[*]”));

int size = abc.size();

int actualSiza = size – 2;

System.out.println(actualSiza);

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

driver.findElement(By.linkText(“next ›”)).click();

if(driver.getPageSource().contains(“Vision…”) == true) {

WebElement element2 = driver.findElement(By.xpath(“//*[@id=’node-2′]/div[1]/div/div/div/p[2]/span/strong”));

System.out.println(“Vison Text : ” + element2.getText());

break;

}else {

System.out.println(“Not found on Page number : ” +i);

}

}

 

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

Part 3 of the code is as below

———————–

Next task is click on “Contact Us” and print the Address in the console.

Contact us is a link so we can simply find the element by linkText locator or partial link text and we can do click operation easily.

Now store the address in a variable and print it on the console by helping getText() method.

Address will print complete address with phone number and email address. And here, as per the question we have to verify that email and phone is available in the address.

So we will use Junit assert, but we need to check the contains inside assertion, for this there is a method assertThat and it takes 2 parameters – one is actual text and matcher (in matcher we pass contains).

To capture the screenshot, we have a method captureScreenShot() in HelperFunation which takes 2 parameters, one is driver reference and another one is file name, then it helps us in taking the screenshots

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

 

Part 3 of code

driver.findElement(By.linkText(“Contact Us”)).click();

WebElement element3 = driver.findElement(By.xpath(“//*[@id=’node-8′]/div/div/div/div/p[4]”));

System.out.println(“Address Text : ” + element3.getText());

try {

Assert.assertThat(element3.getText(), CoreMatchers.containsString(“022-26500208”));

Assert.assertThat(element3.getText(), CoreMatchers.containsString(“uditoffice@gmail.com”));

} catch (AssertionError e) {

System.out.println(“Assertion Failure message : ” + e);

throw e;

}

HelperFunctions.captureScreenShot(driver, “Manipal”);

 

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

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

For our upcoming CP-SAT events, please see the following URL:

http://cpsat.agiletestingalliance.org/index.html#Events

Happy learning and happy test automation

 

 

 

Leave a Reply