How to verify data is saved or not using selenium?



  • enter image description here

    I am using selenium and java for automation. I am adding new data. after a click on save, data will appear on the left side on the top list and I want to verify.
    I want to get the first text "new aam pizza order" for verification that added data is saved or not. my code :

    public class Assigment4 {
    
        String baseUrl="https:// signin";
        WebDriver driver;
    
        @BeforeTest
        public void init() {
            //chrome driver setup
    
            System.setProperty("webdriver.chrome.driver","chromedriver.exe");
            driver =new ChromeDriver();
            driver.get(baseUrl);
            driver.manage().window().maximize();
        }
    
        @Test(priority=2)
        public void createOrderWithoutdata() {
             driver.findElement(By.linkText("Create order")).click();
             driver.findElement(By.id("intent-header-name")).sendKeys("new aam pizza order");
             driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 
             driver.findElement(By.xpath("//button[text()='Save']")).click();
    
    
        }
    


  • How do users verify the data is saved? I would make the test take the same path.

    In your case placing an order might result into an item in the checkout shopping-basket. Find elements in the shopping basket and count them, get their text and validate that with an assert library (JUnit or TestNG).

    Simple example:

    WebDriver driver = new ChromeDriver();
    // the actions
    Assert.assertEquals("new aam pizza order", driver.findElement(By.className("entity")).Text);
    

    Alternativly you could check the database with a query, depending on the data-store you can use a non-Selenium library to retrieve data from it. For accessing databases with SQL and Java research this documentation.

    (Note use database checks with caution. try to test behaviour not implementation. You dont want to update your tests when the storage model is refactored, but nothing changes from the user perspective. Tests should: "Respond to behavior changes. Not respond to structure changes." according to Kent Beck's Programmer Test Principles)

    This line is a clear anti-pattern, try to use explicit waits instead:

         driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 
    

    Read: Is it a bad practice to use implicit wait in Selenium Webdriver? Should one use only explicit wait?



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2