Protected by Copyscape
Powered By Blogger

Saturday, March 27, 2021

How to switch from one window to another using Selenium Java?

The task is very simple if you want to Switch from one window to another using WebDriver object of Selenium API. One has to extract the window id using the following statements

WebDriver driver;

driver=new ChromeDriver();

driver.get("https://www.google.co.in");

String winID=driver.getWindowHandle();

So, driver.getWindowHandle() is the statement where we are extracting the window id using getWindowHandle() method. Simmilarly, if we have multiple Window Ids then we can use the following statement to switch to another window.

driver.switchTo.window(windowid);

Please use the following example to get more information it.

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class WinHdles {
static WebDriver driver;

public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver", "C:\\Browser Executables\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.get("https://opensource-demo.orangehrmlive.com/");

// now we will get the window id by using the below statement
String winID = driver.getWindowHandle();
System.out.println("Current window id is :" + winID);

WebElement lnk = driver.findElement(By.xpath("//*[text()='OrangeHRM, Inc']"));
lnk.click();

//Now after perform click on the above link the new browser window will be opened.
// So, now we will be having multiple windows ids

Set<String> winids = driver.getWindowHandles();

Iterator iterator = winids.iterator();

String strParentWinID = (String) iterator.next();
String strChildWinID = (String) iterator.next();

System.out.println("Parent Window ID is :" + strParentWinID);
System.out.println("Parent Window ID is :" + strChildWinID);

driver.switchTo().window(strChildWinID);
driver.findElement(By.xpath("//*[text()='Contact Sales']")).click();

driver.switchTo().window(strParentWinID);
driver.findElement(By.xpath("//*[@id='txtUsername']")).sendKeys("Admin");
        driver.close();
}
}


***********************************************************************************************************************************************************

I will be adding more concepts in my upcoming posts. Please keep me posted if you have any questions and concerns.



Best of Luck.
 

How many ways we have through which we can generate the Random Numbers in Selenium Java?

 There are 3 ways we have through which we can generate the Random numbers.

1. Java.util.Random class

2. Math.Random method

3. ThreadLocalRandom class

Now we will see that how we can implement the above 3 ways to generate the Random numbers.

Java.util.Random class

To generate the Random number with this way, first we need to create an object of this class like below 

Random rnd=new Random();

To use the above class you need to import the below jar in your project.

import java.util.Random;

Now, we need to see what type of Random number we need to generate like Integer, Long, Boolean, Float & Double etc.

There are methods which we can call through the object of Random class like below

rnd.nextInt(100) // This will generate the Random Number between 0 to 100.

rnd.nextFloat()

rnd.nextLong()

rnd.nextDouble()

rnd.nextBoolean();

import java.util.Random;
public class Numbers
{
public static void main(String args[])
{
Random rnd=new Random();
System.out.println(rnd.nextInt()); //Output 1579708416
System.out.println(rnd.nextInt(100));//Output 36
System.out.println(rnd.nextLong()); //output -3975729109407067933
System.out.println(rnd.nextFloat()); //output 0.22997051
System.out.println(rnd.nextDouble()); //output 0.7115380145242618
System.out.println(rnd.nextBoolean());//output true
}
}

Math.Random method
This method will generate the Random number of type Double between the range of 0.0 to 1.0 . 
import java.util.Random;
public class Numbers
{
public static void main(String args[])
{
System.out.println("Random value :"+ Math.random());
//Output "Random value :0.9302772707882316"
}
}

ThreadLocalRandom class
This class got introduced in Java 1.7 for generating the Random Numbers of type Integer, Float , double, boolean etc. Please find the below example 
import java.util.concurrent.ThreadLocalRandom;
public class Numbers
{
public static void main(String args[])
{

System.out.println("Random value :"+ ThreadLocalRandom.current().nextInt());
System.out.println("Random value :"+ ThreadLocalRandom.current().nextInt(1,10));
System.out.println("Random value :"+ ThreadLocalRandom.current().nextInt(100));
//"Random value :-1778537131
//Random value :6
//Random value :37"
}
}
***********************************************************************************************************************************************************

I will be adding more concepts in my upcoming posts. Please keep me posted if you have any questions and concerns.



Best of Luck.