Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.testng.Assert;
- import org.testng.annotations.BeforeTest;
- import org.testng.annotations.Test;
- public class AutomationTesting {
- final private static String URL = "https://www.seleniumeasy.com/test/basic-first-form-demo.html";
- private static WebDriver driver = null;
- final private static String POPUP_CLOSE_BUTTON = "at-cv-lightbox-close";
- final private static String ENTER_MESSAGE_FIELD = "//input[@id='user-message']";
- final private static String SHOW_MESSAGE_BUTTON =
- "div.container-fluid.text-center:nth-child(2) " +
- "div.row div.col-md-6.text-left:nth-child(2) div.panel.panel-default:nth-child(4) " +
- "div.panel-body form:nth-child(3) > button.btn.btn-default";
- final private static String YOUR_MESSAGE_TEXT_LABEL = "//span[@id='display']";
- final private static String ENTERED_VALUE = "Hello World!";
- final private static String ENTER_A_FIELD = "sum1";
- final private static String ENTER_B_FIELD = "sum2";
- final private static String GET_TOTAL_BUTTON = "//button[contains(text(),'Get Total')]";
- final private static String NUM_VALUE_A = "2";
- final private static String NUM_VALUE_B = "3";
- final private static String STR_VALUE_A = "a";
- final private static String STR_VALUE_B = "b";
- final private static String NAN_VALUE = "NaN";
- private static boolean firstTimeFlag = true;
- @BeforeTest
- public static void init() {
- /*Preconditions*/
- if (firstTimeFlag) {
- System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
- driver = new ChromeDriver();
- driver.get(URL);
- driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
- driver.findElement(By.id(POPUP_CLOSE_BUTTON)).click();
- firstTimeFlag = false;
- }
- }
- @Test
- public static void testShowMessage() {
- String actualYourMessageResult;
- String enteredMessage = ENTERED_VALUE;
- driver.findElement(By.xpath(ENTER_MESSAGE_FIELD)).sendKeys(enteredMessage);
- driver.findElement(By.cssSelector(SHOW_MESSAGE_BUTTON)).click();
- actualYourMessageResult = driver.findElement(By.xpath(YOUR_MESSAGE_TEXT_LABEL)).getText();
- Assert.assertEquals(actualYourMessageResult, enteredMessage);
- }
- @Test
- public static void testGetTotal() {
- int actualDisplayedValue;
- int expectedSumValue = Integer.valueOf(NUM_VALUE_A) + Integer.valueOf(NUM_VALUE_B);
- driver.findElement(By.id(ENTER_A_FIELD)).sendKeys(NUM_VALUE_A);
- driver.findElement(By.id(ENTER_B_FIELD)).sendKeys(NUM_VALUE_B);
- driver.findElement(By.xpath(GET_TOTAL_BUTTON)).click();
- actualDisplayedValue = Integer.valueOf(driver.findElement(By.id("displayvalue")).getText());
- Assert.assertEquals(actualDisplayedValue, expectedSumValue);
- /*Postconditions*/
- driver.findElement(By.id(ENTER_A_FIELD)).clear();
- driver.findElement(By.id(ENTER_B_FIELD)).clear();
- }
- @Test
- public static void testGetTotalNegative() {
- String expectedSumValue = NAN_VALUE;
- String actualDisplayedValue;
- driver.findElement(By.id(ENTER_A_FIELD)).sendKeys(STR_VALUE_A);
- driver.findElement(By.id(ENTER_B_FIELD)).sendKeys(STR_VALUE_B);
- driver.findElement(By.xpath(GET_TOTAL_BUTTON)).click();
- actualDisplayedValue = driver.findElement(By.id("displayvalue")).getText();
- Assert.assertEquals(actualDisplayedValue, expectedSumValue);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement