1. How to apply break from an nested for loop in java?
Lets try this on a Real Time Website -> https://demo.openmrs.org/openmrs/login.htm
- Access the url and enter Admin/Admin123 as credentials
- Select the ‘Inpatient Ward’.
- click on Login.
- Navigate to Appointment Scheduling->Manage Service Types.
- Write Selenium WebDriver Script to verify the Service Type ‘Urology’ available in the html table.
Selenium WebDriver Logic Implemented Using Nested for loop and break Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class NestedForLoopUsingBreak { static ChromeDriver driver; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver","ChromeDriver.exe"); driver = new ChromeDriver(); driver.get("https://demo.openmrs.org/openmrs/appointmentschedulingui/manageAppointmentTypes.page"); driver.findElement(By.id("username")).sendKeys("Admin"); driver.findElement(By.id("password")).sendKeys("Admin123"); driver.findElement(By.id("Inpatient Ward")).click(); driver.findElement(By.xpath("//form[@id='login-form']")).submit(); boolean result = verifyServiceType("Urology"); System.out.println(result); } public static boolean verifyServiceType(String sName) throws InterruptedException { boolean result=false ; List<WebElement> pageList = driver.findElements(By.xpath(".//*[@id='appointmentTypesTable_paginate']/span/a")); List<WebElement> tdList = driver.findElements(By.xpath(".//*[@id='appointmentTypesTable']/tbody/tr/td[1]")); //Example for Nested for loop outerloop: for(int i = 1 ; i< pageList.size();i++) { for(int j=0;j<tdList.size();j++) { if(tdList.get(j).getText().contains(sName)) { System.out.println("Service Type Found!!!"); result = true; break outerloop; } } pageList =driver.findElements(By.xpath(".//*[@id='appointmentTypesTable_paginate']/span/a")); pageList.get(i).click(); tdList = driver.findElements(By.xpath(".//*[@id='appointmentTypesTable']/tbody/tr/td[1]")); } return result; } } |
Output
1 2 3 4 |
Service Type Found!!! true |
=====================================================================================
2. How to Delete a Service Type from the List?
Lets try this on a Real Time Website -> https://demo.openmrs.org/openmrs/login.htm
- Access the url and enter Admin/Admin123 as credentials
- Select the ‘Inpatient Ward’.
- click on Login.
- Navigate to Appointment Scheduling->Manage Service Types.
- Click on Delete icon to perform deletion of Service Type.
- Finding an xpath for Yes button results in multiple matching nodes.
- Even though we applying indexing to identify the ‘Yes’ Button its not working.
Selenium WebDriver Logic Implemented Using isEnabled and is Displayed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class DeleteServiceType { static ChromeDriver driver; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver","ChromeDriver.exe"); driver = new ChromeDriver(); driver.get("https://demo.openmrs.org/openmrs/appointmentschedulingui/manageAppointmentTypes.page"); driver.findElement(By.id("username")).sendKeys("Admin"); driver.findElement(By.id("password")).sendKeys("Admin123"); driver.findElement(By.id("Inpatient Ward")).click(); driver.findElement(By.xpath("//form[@id='login-form']")).submit(); boolean result = deleteServiceType("Dermatology"); System.out.println(result); } public static boolean deleteServiceType(String serviceName) throws InterruptedException { boolean result = false; driver.findElement(By.xpath("//td[text()='"+serviceName+"']/following-sibling::td[3]/descendant::i[@title='Delete']")).click(); List<WebElement> buttonList = driver.findElements(By.xpath(".//*[@id='delete-appointment-type-dialog']/div[2]/button[@class='confirm right']")); System.out.println("Number of Buttons" + buttonList.size()); for(int i = 0;i<buttonList.size();i++) { try{ if(buttonList.get(i).isDisplayed() || buttonList.get(i).isEnabled()) { buttonList.get(i).click(); result= true; break; } } catch(Exception e) { System.out.println("Element is not visible|| enabled"+e.getMessage()); } } return result; } } |
Output
1 2 3 |
true |
Pingback: Selenium WebDriver Tester Resume,Automation Tools Used
Pingback: Sorting the Dynamic HTML Table List in Selenium WebDriver & Java - TotalQA
My question is why was it not working earlier (without using List and isenabled )even though the button was getting highlighted with index