Advertisement
LeonidLS

Untitled

May 31st, 2020
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.50 KB | None | 0 0
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. import org.testng.Assert;
  5. import org.testng.annotations.BeforeTest;
  6. import org.testng.annotations.Test;
  7.  
  8.  
  9. public class AutomationTesting {
  10.  
  11.   final private static String URL = "https://www.seleniumeasy.com/test/basic-first-form-demo.html";
  12.   private static WebDriver driver = null;
  13.   final private static String POPUP_CLOSE_BUTTON = "at-cv-lightbox-close";
  14.   final private static String ENTER_MESSAGE_FIELD = "//input[@id='user-message']";
  15.   final private static String SHOW_MESSAGE_BUTTON =
  16.       "div.container-fluid.text-center:nth-child(2) " +
  17.           "div.row div.col-md-6.text-left:nth-child(2) div.panel.panel-default:nth-child(4) " +
  18.           "div.panel-body form:nth-child(3) > button.btn.btn-default";
  19.   final private static String YOUR_MESSAGE_TEXT_LABEL = "//span[@id='display']";
  20.   final private static String ENTERED_VALUE = "Hello World!";
  21.  
  22.   final private static String ENTER_A_FIELD = "sum1";
  23.   final private static String ENTER_B_FIELD = "sum2";
  24.   final private static String GET_TOTAL_BUTTON = "//button[contains(text(),'Get Total')]";
  25.   final private static String NUM_VALUE_A = "2";
  26.   final private static String NUM_VALUE_B = "3";
  27.   final private static String STR_VALUE_A = "a";
  28.   final private static String STR_VALUE_B = "b";
  29.   final private static String NAN_VALUE = "NaN";
  30.  
  31.   private static boolean firstTimeFlag = true;
  32.  
  33.   @BeforeTest
  34.   public static void init() {
  35.         /*Preconditions*/
  36.     if (firstTimeFlag) {
  37.       System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
  38.       driver = new ChromeDriver();
  39.       driver.get(URL);
  40.       driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
  41.       driver.findElement(By.id(POPUP_CLOSE_BUTTON)).click();
  42.       firstTimeFlag = false;
  43.     }
  44.   }
  45.  
  46.   @Test
  47.   public static void testShowMessage() {
  48.     String actualYourMessageResult;
  49.     String enteredMessage = ENTERED_VALUE;
  50.  
  51.     driver.findElement(By.xpath(ENTER_MESSAGE_FIELD)).sendKeys(enteredMessage);
  52.     driver.findElement(By.cssSelector(SHOW_MESSAGE_BUTTON)).click();
  53.     actualYourMessageResult = driver.findElement(By.xpath(YOUR_MESSAGE_TEXT_LABEL)).getText();
  54.  
  55.     Assert.assertEquals(actualYourMessageResult, enteredMessage);
  56.   }
  57.  
  58.   @Test
  59.   public static void testGetTotal() {
  60.  
  61.     int actualDisplayedValue;
  62.  
  63.     int expectedSumValue = Integer.valueOf(NUM_VALUE_A) + Integer.valueOf(NUM_VALUE_B);
  64.  
  65.     driver.findElement(By.id(ENTER_A_FIELD)).sendKeys(NUM_VALUE_A);
  66.     driver.findElement(By.id(ENTER_B_FIELD)).sendKeys(NUM_VALUE_B);
  67.  
  68.     driver.findElement(By.xpath(GET_TOTAL_BUTTON)).click();
  69.     actualDisplayedValue = Integer.valueOf(driver.findElement(By.id("displayvalue")).getText());
  70.  
  71.     Assert.assertEquals(actualDisplayedValue, expectedSumValue);
  72.  
  73.             /*Postconditions*/
  74.     driver.findElement(By.id(ENTER_A_FIELD)).clear();
  75.     driver.findElement(By.id(ENTER_B_FIELD)).clear();
  76.   }
  77.  
  78.   @Test
  79.   public static void testGetTotalNegative() {
  80.  
  81.     String expectedSumValue = NAN_VALUE;
  82.     String actualDisplayedValue;
  83.  
  84.     driver.findElement(By.id(ENTER_A_FIELD)).sendKeys(STR_VALUE_A);
  85.     driver.findElement(By.id(ENTER_B_FIELD)).sendKeys(STR_VALUE_B);
  86.  
  87.     driver.findElement(By.xpath(GET_TOTAL_BUTTON)).click();
  88.     actualDisplayedValue = driver.findElement(By.id("displayvalue")).getText();
  89.  
  90.     Assert.assertEquals(actualDisplayedValue, expectedSumValue);
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement