CP-SAT MOCK QUESTION SOLUTIONS – 3

 

Problem from the mock exam:

Here is question 3 from the VAPR182 mock exam

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

Open http://maps.google.com and write JUnit / WebDriver test for the following using Google Chrome:

Search for your home address

Print the address text that is displayed in the left frame

Take screenshot of the page at this instant and save the image

Click on direction and get direction from your office address to your home

Display the first option distance and time suggested in eclipse console

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

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 3 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/8y2d37hadgxn1tx/Que3_GoogleMap.java?dl=0

 

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

https://www.dropbox.com/s/rqo1jztwf1nev94/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, another one for WebDriverWait class and last one for Home Address. 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;

String homeAddress;

@Before

public void setUp() throws Exception {

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(15, TimeUnit.SECONDS);

wait = new WebDriverWait(driver, 25);

homeAddress = “Sector 2, Airoli road”;

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

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 maps.google.com website.

It then defines one  By objects (locators for Search Google Maps elements on the page)

I think you can easily inspect the element on the page as most commonly used locators are uniquely available to locate the element on the page.

And, searching home address without clicking on search icon, so we don’t need the element of the seach icon here rather then using keyboard ENTER event.

Please see the below image for your reference:

Other keyboard events also can be used. Those are very powerful.

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.

To print the address from the left frame is little complicated because the address is not a single string, it is devided in three headings.

So we have to check the following-siblings of address then we have to capture all the elements of address into the list.

However, we have to define the string variable and when first time will iterate a loop, we have to check the sring, if it is null then go inside if otherwise go inside else if condition because we have to make a complate address string.

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

Part 2 of Code

// CP-SAT Mock Solution for the reference

@Test

public void test() {

driver.get(“http://maps.google.com”);

By bySrchBox = By.xpath(“//*[@id=\”searchboxinput\”]”);

WebElement weSrchBox = driver.findElement(bySrchBox);

weSrchBox.sendKeys(homeAddress);

// used keyboard event to search

weSrchBox.sendKeys(Keys.ENTER);

// this method is common to take the screenshot

 

By byAddress = By.xpath(“//*[@id=\”pane\”]/div/div[1]/div/div/div[1]/div[3]//following-sibling::h2″);

//By byAddress = By.xpath(“//*[@id=\”pane\”]/div/div[1]/div/div/div[1]/div[3]”);

List<WebElement> weAddress = driver.findElements(byAddress);

String finalAddrss = null;

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

if(finalAddrss == null)

finalAddrss = weAddress.get(i).getText();

else if(finalAddrss != null)

finalAddrss = finalAddrss + ” ” + weAddress.get(i).getText();

}

System.out.println(“Address from Left frame : ” + finalAddrss);

HelperFunctions.captureScreenShot(driver, “HomeAddressScreen”);

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

Part 3 of the code is as below

This is the remaining part of the test method. Now for clicking on the direction we define one by object byDir (locator for direction element on the page). We can use xpath here to locate the element of direction and pass the attribute class inside the xpath that is uniquely available on the page.

To enter the value in choose starting point, create a by object and locate the element using by xpath.

Did not create element for clicking on search icon here as we did earlier, we used keyboard action ENTER.

So here you may face little challenge when we enter the page, it takes time to load so we have to define some explicit wait otherwise your execution may be fail here .

Then simply locate the elements of time and address by using xpath and get the values of the same using getText() method.

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

Part 3 of code

By byDir = By.xpath(“//*[@class=\”section-hero-header-directions-icon\”]”);

//*[@class=”section-hero-header-directions-icon”]

driver.findElement(byDir).click();

By byInput = By.xpath(“//*[@id=\”sb_ifc51\”]/input”);

WebElement weInput = driver.findElement(byInput);

weInput.click();

weInput.sendKeys(“Xperia Mall, Kalyan – Shilphata Road, Opp Lodha World School, Palava City, Dombivli East, Dombivli, Maharashtra 421204”);

weInput.sendKeys(Keys.ENTER);

By byFirstOption = By.xpath(“(//*[@class=’section-directions-trip-title’]/span)[1]”);

By byTime = By.xpath(“//*/div[@class=’section-directions-trip-numbers’]/div[1]/span[1]”);

wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(byFirstOption));

String strFirstOp = driver.findElement(byFirstOption).getText();

String strTime = driver.findElement(byTime).getText();

System.out.println(“Address is : ” + strFirstOp);

System.out.println(“Time taken is : ” + strTime);

====================================.

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