Navigate to: http://the-internet.herokuapp.com/dynamic_controls
- Click on Remove Check box button.
- wait for check box to be removed
- Validate for message “its Gone”
- Click on Add button.
- Wait for Check box to re-appear.
- Validate message Check box is back
Part 2:
- Check text box is disabled.
- If disabled then, click on Enable.
- Wait for it to become enable
- Validate if its enabled
- Enter the text “ur name”
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
//click on Remove button
WebElement remove_button = driver.findElement(By.xpath("//button[@type='button' and text()='Remove']"));
remove_button.click();
//Explicit wait
//Thread.sleep(10000);
//dynamic wait
WebDriverWait wait= new WebDriverWait(driver,20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='message']")));
//Fetching Message
WebElement str = driver.findElement(By.xpath("//*[@id='message']"));
System.out.println(str.getText());
//click on Enable button
WebElement Enable_button = driver.findElement(By.xpath("//button[@type='button' and text()='Enable']"));
Enable_button.click();
//dynamic wait
WebDriverWait wait1= new WebDriverWait(driver,20);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='message']")));
//Fetching Message
WebElement str1 = driver.findElement(By.xpath("//*[@id='message']"));
System.out.println(str1.getText());
//Printing message
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Rahul..!!!");
}
Using try catch and For loop
Start your code here
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
//click on Remove button
WebElement remove_button = driver.findElement(By.xpath("//button[@type='button' and text()='Remove']"));
remove_button.click();
for(int i=0;i<10;i++) {
try {
WebElement str1 = driver.findElement(By.xpath("//*[@id='message']"));
System.out.println(str1.getText());
break;
}catch(Exception e) {
Thread.sleep(1000);
}
}
Synchronization using try catch block.
public class DynamicControl {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Suraj Gaikwad\\JSScriptPopUp\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
WebElement remove_button = driver.findElement(By.xpath("//button[text()='Remove']"));
remove_button.click();
for(int i=0;i<10;i++){
try {
WebElement its_gone_text = driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(its_gone_text.getText());
break;
} catch (Exception e) {
Thread.sleep(1000);
}
}
WebElement enable_button = driver.findElement(By.xpath("//button[text()='Enable']"));
enable_button.click();
for(int i=0;i<10;i++){
try {
WebElement enabled_text = driver.findElement(By.xpath("//form[@id='input-example']//p[@id='message']"));
System.out.println(enabled_text.getText());
break;
} catch (Exception e) {
Thread.sleep(1000);
}
}
}
}
1.This code generates NoSuchElementException
public static void main(String[] args) throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
//click on remove
driver.findElement(By.xpath("//button[text()='Remove']")).click();
WebElement element=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(element.getText());
2.to handle this Exception using try catch and for loop
public class Dynamic_controls
{
public static void main(String[] args) throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
//Part 1
//click on remove button
driver.findElement(By.xpath("//button[text()='Remove']")).click();
//fetch data by using getText() method
for(int i=0;i<10;i++)
{
try
{
WebElement element=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(element.getText());
break;
}
catch(Exception e)
{
Thread.sleep(1000);
}
}
//Part 2
driver.findElement(By.xpath("//button[text()='Enable']")).click();
for(int a=0;a<10;a++)
{
try
{
WebElement ele=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(ele.getText());
break;
}
catch(Exception e)
{
Thread.sleep(1000);
}
}
}
}
public class Dynamic_controls {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
WebDriverWait wait;
System.setProperty(“webdriver.chrome.driver”, “E:\\chromedriver.exe”);
driver = new ChromeDriver(); //upcasting
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(6500, TimeUnit.SECONDS);
//driver.manage().timeouts().implicitlyWait(4000, TimeUnit.SECONDS);
driver.get(“http://the-internet.herokuapp.com/dynamic_controls”);
WebElement Remove_btn=driver.findElement(By.xpath(“//button[text()=’Remove’]”));
Remove_btn.click();
for(int i=0; i<10;i++)
{
try{
//WebElement Add_btn=driver.findElement(By.xpath(“//button[@type=’button’]”));
//Add_btn.click();
WebElement s=driver.findElement(By.xpath(“//p[@id=’message’]”));
System.out.println(s.getText());
}
catch(Exception e)
{
Thread.sleep(3000);
}
}
WebElement enable_btn=driver.findElement(By.xpath(“//button[text()=’Enable’]”));
enable_btn.click();
wait = new WebDriverWait(driver, 5);
//WebElement str1=driver.findElement(By.xpath(“//p[@id=’message’]”));
WebElement str1=wait.until(ExpectedConditions.presenceOfElementLocated((By.xpath(“//p[@id=’message’]”))));
System.out.println(str1.getText());
WebElement Text_box=driver.findElement(By.xpath(“//input[@type=’text’]”));
{
if(Text_box.isEnabled())
{
Text_box.sendKeys(“visionit”);
}
else
{
System.out.println(“it is disable”);
}
}
}
}
Dynamic control using WebDriverwait
public class Dynamic_control {
public static void main(String[] args) throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
//part 1
WebDriverWait wait=new WebDriverWait(driver,30);
//click on remove button
driver.findElement(By.xpath("//button[text()='Remove']")).click();
//fetch message it's gone!
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@id='message']")));
WebElement ele1=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(ele1.getText());
//click on add button
driver.findElement(By.xpath(("//button[text()='Add']"))).click();
//fetch message it's back!
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@id='message']")));
WebElement ele2=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(ele2.getText());
//Part 2
//click on enable button
driver.findElement(By.xpath("//button[@onclick='swapInput()']")).click();
//fetch message
WebDriverWait wait1=new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@id='message']")));
WebElement ele3=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(ele3.getText());
driver.findElement(By.xpath("//*[@type='text']")).sendKeys("VisionIt");
driver.findElement(By.xpath("//button[@onclick='swapInput()']")).click();
//fetch message
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@id='message']")));
WebElement ele4=driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(ele4.getText());
}
}
dynamic control assignment -
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
driver.manage().window().maximize();
WebElement remove_button = driver.findElement(By.xpath("//button[text()='Remove']"));
remove_button.click();
for (int i = 0; i <= 5; i++) {
try {
WebElement remove_msg = driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(remove_msg.getText());
} catch (Exception e) {
// TODO: handle exception
Thread.sleep(1000);
}
}
WebElement add_btn = driver.findElement(By.xpath("//button[text()='Add']"));
add_btn.click();
for (int i = 0; i <= 5; i++) {
try {
WebElement add_btn_msg = driver.findElement(By.xpath("//form[@id='checkbox-example']//p[@id='message']"));
System.out.println(add_btn_msg.getText());
}
catch (Exception e) {
// TODO: handle exception
Thread.sleep(1000);
}
}
WebElement enable_button = driver.findElement(By.xpath("//button[text()='Enable']"));
enable_button.click();
for(int i=0;i<5;i++){
try {
WebElement enabled_text = driver.findElement(By.xpath("//form[@id='input-example']//p[@id='message']"));
System.out.println(enabled_text.getText());
break;
} catch (Exception e) {
Thread.sleep(1000);
}
}
WebElement disable_button = driver.findElement(By.xpath("//button[text()='Disable']"));
disable_button.click();
for(int i=0;i<5;i++){
try {
WebElement disable_text = driver.findElement(By.xpath("//p[@id='message']"));
System.out.println(disable_text.getText());
break;
}
catch (Exception e) {
Thread.sleep(1000);
}
}