MainMenu

Home Java Overview Maven Tutorials

Monday 16 May 2022

JavascriptExecutor usage in selenium

In this article we will see how to perform multiple operations like scroll, click, getText, sendText using java script executor

If a web page is highly dynamic in nature it have complex Javascript functions or Ajax function then sometimes simple click using selenium like
driver.findElement(By.xpath("")).click(); do not work OR many functions like getText(), sendKeys() etc do not work
So here we can use JavascriptExecutorInterface and its various mathods to perform such operations.

How to perform Scroll by using java script executor

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(X,Y)", "");
//here X & Y are value in pixal

Example :- JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");


public static void scrollxandy(WebDriver driver, int x, int y)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy("+x+","+y+")", "");
}


How to perform Scroll upto a webElement by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


How to perform Scroll upto upto bottom of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");



public static void scrolluptobottom(WebDriver driver)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}


How to perform Scroll upto upto Top of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, 0);");


WebElement element = driver.findElement(By.tagName("header"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, -1000000000)");


How to perform Action click by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", we);


How to perform Action SendKeys/SendText by using java script executor


public static void sendTextusingJavascriptExecutor(WebDriver driver, WebElement we, String text)
{
String inputtext = "arguments[0].value='"+text+"';";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(inputtext, we);
}



How to perform Action getText by using java script executor


public static String getTextUsingJavascriptExecutor(WebDriver driver, WebElement we)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
String text = (String) jse.executeScript("return arguments[0].value", we);
return text;
}




How to Highlight a webElement by using java script executor


public static void highlight(WebDriver driver, WebElement element) throws Exception
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
Thread.sleep(100);
jse.executeScript("argument[0].setAtrribute('style', '');", element);
Thread.sleep(100);
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
}




Sunday 1 May 2022

How to Press Multiple Keys in Selenium


How to press mulitple keys in selenium

Example :- Press control + t key :



driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 't');

OR

driver.findElement(By.xpath("")).sendKeys(Keys.chord(Keys.CONTROL, "T"));

------------------------------------
How to press Enter keys in selenium
WebElement textbox = driver.findElement(By.id("idOfElement"));

textbox.sendKeys(Keys.ENTER);

How to Press thress keys simultaneously in selenium


driver.findElement(By.xpath("")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ALT, Keys.DELETE));

How to Validate input/character is number or string in Java

Validate Input String is number or not



Hello Friends,

This article will describe how can we check whether a data is number or String in java.



Algorithm :-
1). First Break the string into characterArray. 2) Now to check for number use Integer.Parseint() 3) Use Character.isDigit() to check whether character is number or not.


Example :-
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class PojorunExample
{
static boolean isnumber(String x)
{
boolean flag = false;
try
{
int num = Integer.parseInt(x);
flag = true;
}
catch(Exception e)
{
}
return flag;
}
@Test
public void g()
{
String x = "Chandan";
String y = "chandan30";
if(isnumber(x))
{
System.out.println("its a number");
}
else
{
System.out.println("its not a number");
}
char[] ch = y.toCharArray();
for(int i = 0; i<ch.length; i++)
{
if(Character.isDigit(ch[i]))
{
System.out.println(ch[i] + " " + "this is number");
}
else
{
System.out.println(ch[i] + " " + "this is letter");
}
}
}
}
Output :-
its not a number
c this is letter
h this is letter
a this is letter
n this is letter
d this is letter
a this is letter
n this is letter
3 this is number
0 this is number