2

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();


    }

2 Answers 2

7

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?

1
  • I want to know the best practice of doing automation. If I have 100 test cases are there to automate, so I am creating 100 methods and putting priority 1 to 100. am I doing right or not? or any better way to do
    – Aam ali
    Oct 14, 2019 at 15:07
2

I agree with Niels` suggestion of checking the database. An alternative could be refreshing the page, or navigating back and forth and asserting that you still see the value. That would imply that the data is stored.

But before that I would suggest talking with your development team (if that is the case here) and see if this check for storing in the database is something that can be testef in a unit or integration test. When it is tested in unit and integration tests you can assume that when you see it on the screen, it is stored correctly. That makes your Selenium tests shorter, less complex, and thus cheaper. See https://martinfowler.com/articles/practical-test-pyramid.html for some more context on the test pyramid.

1
  • 1
    I did add a warning to my answer, because checking the database should be a last resort. I do not prefer to test using implementation details. Oct 14, 2019 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.