Skip to main content

Complex scenarios automation in Selenium- Part 1

So for this exercise we will be clicking an any state in this SVG map.

https://petdiseasealerts.org/forecast-map#/




So for this exercise, the best chance you getting a valid result is getting the correct xpath. So most of you will get this xpath.

String stateXpath =
"(//*[name()='svg'])/*[@id='features']/*[@id='regions']/*[@id='"+state+"']";

or this

String stateXpath= "//*[name()='svg']//*[name()='g' and @id = ' "+state+" ']";

with one of this implementations if you ran following code, you will fail at state Florida, with element click intercept exception.

package org.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;

public static void main( String[] args ) throws InterruptedException {

String state ="Georgia";
String stateXpath =
"(//*[name()='svg'])/*[@id='features']/*[@id='regions']/*[@id='"+state+"']";
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");



ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);

driver.get("https://petdiseasealerts.org/forecast-map#/");

driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");

Thread.sleep(10000);



//Implementation 1
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
driver.findElement(By.xpath(stateXpath)).click();

}
}



But if you get the correct xpath there won't be any issue in automating any state in here. Also need to keep in mind that we have to use js.executeScript to execute the click, normal click will not work for this scenario.

package org.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;

public static void main( String[] args ) throws InterruptedException {

String state ="Georgia";
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");




ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);

driver.get("https://petdiseasealerts.org/forecast-map#/");

driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");

Thread.sleep(10000);


//Implementation 2
String commonXpath = "//*[@id='regions']/*[@class='region']";
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
List<WebElement> statesList = driver.findElements(By.xpath(commonXpath));
System.out.println("States count: "+ statesList.size());

String newXpath = commonXpath +
"/*[@class='rpath']/*[contains(@name,'"+state+"')]";
js.executeScript("arguments[0].dispatchEvent(new Event('click'))",
driver.findElement(By.xpath(newXpath)));



}
}

Thats the best solution for this scenario.


Comments

Popular posts from this blog

How to create Push Notification System for your Cordova app with OneSignal and Firebase

H ello Everyone, today i'm going to show you how to add push notification system for your Cordova mobile app with OneSignal. First let's talk little bit about OneSignal. OneSignal is a multi-platform push notification service, which gives you variety of features. It lets you to send push notifications to almost all current mobile Operating Systems. You can check out their documentation if you want or start straightly this tutorial. If you need to see the project structure  and stuff  go to github repo Lets begin our tutorial 😎😎😎 First lets create a Cordova project. type following command in your CMD.    C:\Users\acer>cordova create push There will be a structure like this,      Now run android emulator, if you don't know how to run emulator from your CMD see this tutorial and do it exactly like that. Go to your project folder and then type the following command in your CMD  C:\Users\acer\push>cordova platfor...

Creating a fully functional page object model automation framework with Selenium in 1 hour

In this tutorial we will check how to create a simple Selenium/ Testng page object model framework with reporting and logging capabilities. For this I'm using Intellij idea IDE Community version because of its amazing debugging capabilities. Also I'm using Maven, Selenium with Testng with log4j. Let's begin, 1. Create a Maven project using intellij IDE. 2. Prepare the project structure as follows, There should be 4 folders/packages to handle functions, pages, resources and test cases.  Before that delete the existing auto org.example folders in both main and test folders. Then created following folders as below. 3. Now we need to download all the necessary packages we need to use in this project. Paste following mvn dependencies in your pom.xml file within <dependencies></dependencies> tags. before that remove this junit dependency which is already coming automatically because we don't need it. <dependency> <groupId> junit </groupId> ...

How to deploy Android Emulator from CMD in Windows

First go to your C drive Then find Users folder and your Personal folder in my case acer Then go to .android folder Now first you have to check what are the available emulators first emulator -list-avds you will get some thing like this, if not you have to create emulator device first .  Now run following command by specifying which device you want. For me i will pick Nexus_5_API_24 emulator -avd Nexus_5_API_24 And your job is done.