Advertisement
DSTRN

SearchTest

Jan 17th, 2022
746
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import static org.junit.Assert.assertEquals;
  2. import static org.testng.Assert.assertTrue;
  3.  
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.chrome.ChromeDriver;
  9. import org.openqa.selenium.support.PageFactory;
  10.  
  11. import rs.ac.uns.kts.pages.HomePage;
  12. import rs.ac.uns.kts.pages.SearchResultsPage;
  13.  
  14. public class SearchTest {
  15.  
  16.     private WebDriver driver;
  17.  
  18.     private HomePage homePage;
  19.     private SearchResultsPage searchResultsPage;
  20.  
  21.     @Before
  22.     public void setupSelenium() {
  23.         // instantiate driver
  24.         System.setProperty("webdriver.chrome.driver", "chromedriver97.exe");
  25.         driver = new ChromeDriver();
  26.         // maximize window
  27.         driver.manage().window().maximize();
  28.         // navigate
  29.         driver.navigate().to("http://automationpractice.com/index.php");
  30.  
  31.         homePage = PageFactory.initElements(driver, HomePage.class);
  32.         searchResultsPage = PageFactory.initElements(driver, SearchResultsPage.class);
  33.     }
  34.  
  35.     @Test
  36.     public void searchTest() {
  37.         // try empty search
  38.         homePage.submitSearchBtnClick();
  39.  
  40.         assertEquals(
  41.                 "http://automationpractice.com/index.php?controller=search&orderby=position&orderway=desc&search_query=&submit_search=",
  42.                 driver.getCurrentUrl());
  43.  
  44.         assertTrue(searchResultsPage.errorMessagePresent("Please enter a search keyword"));
  45.         assertTrue(searchResultsPage.resultMessagePresent("0 results have been found."));
  46.  
  47.         // try non existing search
  48.         homePage.setSearchInput("Non existing term");
  49.         homePage.submitSearchBtnClick();
  50.  
  51.         assertEquals(
  52.                 "http://automationpractice.com/index.php?controller=search&orderby=position&orderway=desc&search_query=Non+existing+term&submit_search=",
  53.                 driver.getCurrentUrl());
  54.  
  55.         assertTrue(
  56.                 searchResultsPage.errorMessagePresent("No results were found for your search \"Non existing term\""));
  57.         assertTrue(searchResultsPage.resultMessagePresent("0 results have been found."));
  58.  
  59.         // try existing term
  60.         homePage.setSearchInput("dress");
  61.         homePage.submitSearchBtnClick();
  62.  
  63.         assertEquals(
  64.                 "http://automationpractice.com/index.php?controller=search&orderby=position&orderway=desc&search_query=dress&submit_search=",
  65.                 driver.getCurrentUrl());
  66.  
  67.         assertTrue(searchResultsPage.productContainsText("Dress"));
  68.  
  69.     }
  70.  
  71.     @After
  72.     public void closeSelenium() {
  73.         // Shutdown the driver
  74.         driver.quit();
  75.     }
  76. }
  77.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement