samedi 28 mars 2020

Selenium: How do I navigate to a new page and do assertions there?

I'm new to Selenium. I'm currently trying to test a web application.

I want to click a button on the start page, which navigates me to a login page. There I'd like to enter my credentials and click the login button. This leads me to a page where I'd like to assert some things.

The problem is, that whenever I've navigated to a new page, I the test doesn't work as expected. Example: I get to the Login Page, but then the input fields aren't filled in. If I go to the Login page directly, it works.

I assume this is a test case that many people will have had, so there must be a simple solution to it, but I just can't manage to find one. I'd really appreciate any help a lot.

Here is my test code:

public class AdminTest {
    protected WebDriver driver; 

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
        ChromeOptions chromeOptions = new ChromeOptions();

        this.driver = new ChromeDriver(chromeOptions);    

    }

    @After
    public void tearDown() throws Exception {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void viewOrderListTest() {

        // this leads to the start page
        this.driver.get("http://localhost:8080/eStore/");
        this.driver.manage().window().maximize();

        // I click the link to the Login page, so far so good
        this.driver.findElement(By.id("link_to_login")).click();

        this.driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // the keys are NOT entered
        WebElement username = this.driver.findElement(By.id("username"));

        username.click();
        username.clear();
        username.sendKeys("admin");

        WebElement password = this.driver.findElement(By.id("password"));
        password.click();
        password.clear();
        password.sendKeys("admin");

        // This button gets clicked by the test
        this.driver.findElement(By.id("submit_button")).click();

Aucun commentaire:

Enregistrer un commentaire