1
      @Test(priority = 0)
      public void test() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("lanka@ensiz.com");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("123456");
        driver.findElement(By.xpath("//button[contains(text(),'Sign In')]")).click();

      }
      @Test(priority = 1)
      public void verifyHomepageTitle(){
              String expectedTitle = "Placer Admin - Home";
              String actualTitle = driver.getTitle();
              Assert.assertEquals(actualTitle, expectedTitle);     
      }
      @AfterClass(alwaysRun = true)
      public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
          fail(verificationErrorString);
        }
      }    

I'm new to automation testing.I want to make sure the valid user logins.For that i'm trying to verify the title of the page.But my test fail all the time,because it is execute before valid user going to the dashboard.how can I test this?Can i know the proper modifications for this code?

Please help..thanks

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
user3806999
  • 39
  • 4
  • 14

3 Answers3

1

What you can do is, on the dashboard page select an element which is always there but is not on the login page. For example, a menu item or maybe a header.

Then create a wait like this at the end of the login test, so the test only completes after the dashboard is loaded:

 WebDriverWait wait = new WebDriverWait(driver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPath here")));

This will wait for a max of 5 seconds for the dashboard to load. The syntax could be wrong somewhere as I do everything in C#.

Anand
  • 1,899
  • 1
  • 13
  • 23
  • It is not such a good idea to mix implicit and explicit waits - https://stackoverflow.com/questions/29474296/clarification-of-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc – Grasshopper Apr 19 '18 at 10:15
  • @Anand Thanku very much,I have locate it to navbar.This works,thanks again – user3806999 Apr 19 '18 at 10:29
1

Use this code it is working fine in my machine :

public class User3806999 {

    WebDriver driver;
    WebDriverWait wait;

    @BeforeClass
    public void setUpClass(){
        System.setProperty("webdriver.chrome.driver", "F:\\Automation\\chromedriver.exe");
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver,30); 
        driver.manage().window().maximize();
        driver.get("https://test.admin.placer.life/login");
    }

    @Test()
    public void testLogin() throws Exception {
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("lanka@ensiz.com");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("123456");
        driver.findElement(By.xpath("//button[contains(text(),'Sign In')]")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("user_menu"))));
        //Assert something here 
    }

    @Test(dependsOnMethods ={"testLogin"})
    public void verifyHomepageTitle(){
            String expectedTitle = "Placer Admin - Home";
            String actualTitle = driver.getTitle();
            Assert.assertEquals(actualTitle, expectedTitle);     
    }

    @AfterClass(alwaysRun = true)
    public void tearDown() throws Exception {
          //logout here 

      }
}
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • It is not such a good idea to mix implicit and explicit waits - https://stackoverflow.com/questions/29474296/clarification-of-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc – Grasshopper Apr 19 '18 at 10:15
  • @Grasshopper : I agree with you ! I'll remove that line – cruisepandey Apr 19 '18 at 10:18
  • @Grasshopper : here is a problem , if I'm removing the implicit wait then , these two methods are failing , but if I'll keep implicit wait along with explicit wait then the tests are getting passed any insights ? – cruisepandey Apr 19 '18 at 10:24
  • Set the implicit wait to 0, before calling explicit wait. After explicit wait set implicit wait back to the original value. You can also create a util method which handles this for all explicit wait. – Grasshopper Apr 19 '18 at 10:26
  • @Grasshopper : wouldn't that be a mixture of waits ? – cruisepandey Apr 19 '18 at 10:30
  • That is actually the default timeout for implicit wait. Refer to this section on implicit wait - https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp – Grasshopper Apr 19 '18 at 10:39
0

Use the explicit wait as below before assertion:

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains(expectedTitle);
F. Müller
  • 3,969
  • 8
  • 38
  • 49