6

I am building a testing framework for my website I want to fully separate the framework away from test the issue is when I write a test sometimes the Assert needs time until it can be true, for example if I am on Upload file page and when the file is uploaded the website should display File uploaded successfully page but it will need to much time until the browser reaches this page

How should I force the Assert to wait sometime before it returns result?

some code that might explain my current way of work:

Upload Page Class

Public class UploadPage
{
 [FindsBy(How = How.Name, Using = "upload-button")]
 public IWebElement BtnUpload { get; set; }

  public UploadPage()
  {
   PageFactory.InitElements(Driver, this);
  }

 public void UploadFile(string path)
  {
    //select file
    BtnUpload.Click();
  }
}

Successful Upload Page:

Public class UploadSuccessfulPage
   {
     [FindsBy(How = How.Name, Using = "success-message")]
     public IWebElement LblSuccessMessage{ get; set; }

     public UploadSuccessfulPage()
     {
       PageFactory.InitElements(Driver, this);
     }
     public bool IsAt()
     {
      Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60))
      //LblSuccessMessage can be found on page
     }
   } 

Test Method:

public void TestUpload()
{
 UploadPage uploadPage= new UploadPage ();
 uploadPage.UploadFile(path);
 UploadSuccessfulPage successPage= new UploadSuccessfulPage();
 Assert.IsTrue(successPage.IsAt());
}

when I write my tests this way the assert do not wait despite that IsAt() contains an implicit wait.

P.S: I am not intending to use Thread.Sleep();.

3
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60))

implicit wait is a normally defined when a driver is created, try relocating the code above to where you created your browser instance. E.g.

Driver = new FirefoxDriver();
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
  • I am using a static class for the Driver, anyway how will this fix my issue? – Yahya Hussein Dec 21 '16 at 15:41
  • Read up on waits in Selenium – kirbycope Dec 21 '16 at 16:54
  • In many cases I observed[Personal Experience] - That solution its not worked for me :( – NarendraC Dec 22 '16 at 7:46
3

Good practice - That you are trying to stick with Implicit wait. It may happen at some instance you have to use explicit wait.[Keep in mind]

  • Wait for element

    // wait for field
    
    WebDriverWait wait = new WebDriverWait(driver, 90);
    wait.until(ExpectedConditions.visibilityOf(element));
    
  • Wait for page to get load

    // Wait till page get loaded completely
    
    static void waitForPageLoad(WebDriver wdriver) {
      WebDriverWait wait = new WebDriverWait(wdriver, 60);
    
        Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {
    
        @Override
        public boolean apply(WebDriver input) {
            return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");
        }
    
      };
    wait.until(pageLoaded);
    
    }
    
0

Well might be too late but you might find some use later.

'_driver = new FirefoxDriver();
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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