Friday, 4 August 2017

Code Example - Check Box isSelected

package com.seleniumWebdriver;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class CheckBoxExample {

private WebDriver driver;
private String basePageURL;

public void Select_The_Checkbox(WebElement element) {
try {
if (element.isSelected()) {
System.out.println("Checkbox: " + element + "is already selected");
} else {
// Select the checkbox
element.click();
}
}
catch (Exception e) {
System.out.println("Unable to select the checkbox: " + element);
}
}

public void DeSelect_The_Checkbox(WebElement element) {
try {
if (element.isSelected()) {
// De-select the checkbox
element.click();
} else {
System.out.println("Checkbox: " + element + "is already deselected");
}
}
catch (Exception e) {
System.out.println("Unable to deselect checkbox: " + element);
}
}

public void Select_The_CheckBox_from_List(WebElement element, String valueToSelect) {
List<WebElement> allOptions = element.findElements(By.tagName("input"));
for (WebElement option : allOptions) {
System.out.println("Option value " + option.getText());
if (valueToSelect.equals(option.getText())) {
option.click();
break;
}
}
}

@Test
public void testCaseToCheck() {
System.setProperty("webdriver.gecko.driver", "./Browser_Driver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(basePageURL);
WebElement checkBoxElement = driver.findElement(By.id("persist_box"));
// Wait for the checkbox element to be visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(checkBoxElement));
Select_The_Checkbox(checkBoxElement);
}

@Test
public void testCaseToUnCheck() {
driver.navigate().to(basePageURL);
WebElement checkBoxElement = driver.findElement(By.id("persist_box"));
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(checkBoxElement));
DeSelect_The_Checkbox(checkBoxElement);
}

@Test
public void testCaseToCheckDesired() {
driver.navigate().to("someother page");
WebElement element = driver.findElement(By.cssSelector(".display"));
Select_The_CheckBox_from_List(element, "soccer");
}
}

No comments:

Post a Comment

Code Example - File Download By Robot Class In Firefox Browser

Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...