Skip to content

Instantly share code, notes, and snippets.

@ShubhamS32
Created April 22, 2019 05:59
Show Gist options
  • Select an option

  • Save ShubhamS32/23878ad8d5bd6b57e67c784e39052e17 to your computer and use it in GitHub Desktop.

Select an option

Save ShubhamS32/23878ad8d5bd6b57e67c784e39052e17 to your computer and use it in GitHub Desktop.
A small demo of Selenium using java 8 to show the use case of multi-windows pop ups and accessing the data within them.
/**
*
*/
package com.selenium.demo;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Iterator;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author Shubham
*
*/
class Selenim_Demo {
@Test
void test() {
String ChromDriver = "D:\\Selenium-Automation\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromDriver);
WebDriver browser = new ChromeDriver();
// Navigate Browser to MT URL
String MT_URL = "D://Selenium-Automation//index.html";
browser.get(MT_URL);
//Get the Main Window Id to maintain the to and fro between the windows.
String MainWindow=browser.getWindowHandle();
System.out.println("MainWindow id is >"+MainWindow);
//Click on the p tag
browser.findElement(By.xpath("//*[@id=\"p_click\"]")).click();
waitForPageFullyLoaded(2, 1,browser);
//Now Iterating over browser to get the child windows:
// To handle all new opened window.
Set<String> s1=browser.getWindowHandles();
Iterator<String> i1=s1.iterator();
System.out.println("Before While Loop");
while(i1.hasNext())
{
String ChildWindow=i1.next();
System.out.println("Inside While Loop");
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
System.out.println("Inside Child Window");
// Switching to Child window
browser.switchTo().window(ChildWindow);
//Entering the value
System.out.println("Setting Value");
browser.findElement(By.xpath("//*[@id=\"p_text\"]"))
.sendKeys("Shubham");
//Submitting the value to MainWindow
browser.findElement(By.xpath("//*[@id=\"p_click\"]")).click();
}
}
//Back to Main Windows
}
/**
* Soft wait for loading of all elements in page. This will check for count
* of loaded elements in page in every specified timeSlice interval till
* maxTimeout is achieved. Once count remains same for two consecutive
* checks, it assumes all elements loaded and returns true.
* @author Shubham
* @param maxtimeout
* Max time to wait in seconds
* @param timeSlice
* slice of time interval to check for elements count in seconds.
* @return
*/
public boolean waitForPageFullyLoaded(int maxtimeout, int timeSlice,WebDriver browser) {
int previous;
int current = 0;
do {
previous = current;
sleep(timeSlice);
maxtimeout -= timeSlice;
current = browser.findElements(By.xpath("//*")).size();
} while (current != previous && maxtimeout > 0);
if (maxtimeout > 0) {
return true;
}
//Log.error("Page didnt load completely in "+maxtimeout+" seconds.");
return false;
}
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of seconds, subject to the precision
* and accuracy of system timers and schedulers. The thread does not lose
* ownership of any monitors.
*
* @param timeout
* the length of time to sleep in seconds
* @author Shubham
*/
public void sleep(int timeout) {
try {
Thread.sleep(timeout * 1000);
} catch (Exception e) {
//Log.error("Interrupted Exception Occurred in sleep.");
//Log.debug(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment