Advertisement
nazar_art

new XPath

Oct 24th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package selenium.web.driver;
  2.  
  3. import static org.junit.Assert.assertTrue;
  4.  
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.firefox.FirefoxDriver;
  12.  
  13.  
  14. public class OMS_selenium_Test {
  15.     WebDriver driver;
  16.     WebElement element;
  17.    
  18.     @Before
  19.     public void setUp() throws Exception {
  20.         driver = new FirefoxDriver();
  21.         driver.get("http://localhost:8180/OMS/login.htm");
  22.         // precondition
  23.         element = driver.findElement(By.name("j_username"));
  24.         element.sendKeys("login1");
  25.         element = driver.findElement(By.name("j_password"));
  26.         element.sendKeys("qwerty");
  27.         element.submit();
  28.         // waiting
  29.         try {
  30.             Thread.sleep(2000);
  31.         } catch (InterruptedException e) {
  32.             System.out.println("InterruptedException occured");
  33.         }
  34.         // going to ordering page
  35.         driver.get("http://localhost:8180/OMS/order.htm");
  36.     }
  37.    
  38.     @Test
  39.     public void test_Status_Delivered() {
  40.         // choose Status from search drop down list
  41.         driver.findElement(By.xpath("//option[@value='orderStatus']")).click();
  42.        
  43.         // choose Delivered from criteria to Status
  44.         driver.findElement(By.xpath("//option[@value='Delivered']")).click();
  45.        
  46.         // click Apply button
  47.         driver.findElement(By.name("Apply")).click();
  48.         try {
  49.             Thread.sleep(3000);
  50.         } catch (InterruptedException e) {
  51.             System.out.println("InterruptedException occured");
  52.         }
  53.        
  54.         // checking results
  55.         String actualResultOne = driver.findElement(By.xpath("//DIV[@id = 'list']//TABLE//TR//TD[.//text()[starts-with(., 'OrderName1')]]")).getText();
  56.         String actualResultTwo = driver.findElement(By.xpath("//DIV[@id = 'list']//TABLE//TR//TD[.//text()[starts-with(., 'OrderName5')]]")).getText();
  57.        
  58.         assertTrue("Test run successefull", actualResultOne.equals("OrderName1") && actualResultTwo.equals("OrderName5"));
  59.     }
  60.    
  61.     @Test
  62.     public void test_SearchBy_OrderName() {
  63.         // choose OrderName from search drop down list
  64.         driver.findElement(By.xpath("id('search')/option[@value='orderName']")).click();
  65.         // enter value into field
  66.         element = driver.findElement(By.name("searchValue"));
  67.         element.sendKeys("OrderName1");
  68.         // click Apply button
  69.         driver.findElement(By.name("Apply")).click();
  70.         try {
  71.             Thread.sleep(3000);
  72.         } catch (InterruptedException e) {
  73.             System.out.println("InterruptedException occured");
  74.         }
  75.        
  76.         // compare results     
  77.         String actualResult = driver.findElement(By.xpath("//DIV[@id = 'list']//TABLE//TR//TD[.//text()[starts-with(., 'OrderName1')]]")).getText();
  78.         assertTrue("Test run successefull", actualResult.equals("OrderName1"));
  79.     }
  80.    
  81.     @After
  82.     public void tearDown() throws Exception {
  83.         // log out
  84.         element = driver.findElement(By.xpath("//UL[@id = 'nav']//LI[contains(concat(' ', @class, ' '), ' spec ')]//A[contains(concat(' ', @class, ' '), ' spec ')]"));
  85.         // quit browser
  86.         driver.quit();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement