0

My code is running too quickly and waits for URL verification and elements are not helping. These are the waits I have tried

wait.Until(ExpectedConditions.ElementToBeClickable(activationCodePage.ConfirmButton()));

wait.Until(e => e.Url.Contains("verify/activation-code"));

I have even tried a recommendation from a previous stack

But the only thing that seems to work is a Thread.sleep() for 10 seconds and I don't / can't use that for my tests.

Here is the code

  verifyEmailPage.ConfirmDepartmentEmailButton().SendKeys(deptEmail);
  verifyEmailPage.VerifyEmailPageButton().Click();
   
  //This is where I run into issues, the page and requests haven't completely finished loadingby the
  //Time I begin to call my task, this is where I tried both waits, the wait from the previous Thread
  //IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver,TimeSpan.FromSeconds(30.00));
  //wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
  //and none of these have seemed to have work only Thread.Sleep(10000)

  var activationCode = await ActivationCodeService.GetLatestActivationCodeByEmailAsync(deptEmail);

  var activationCodePage = new VerifyActivationCodePage(driver);
  activationCodePage.ActivationCodeInput().SendKeys(activationCode.Code);

Here is the code for

GetLatestActivationCodeByEmailAsync()

public async Task<ActivationCode> GetLatestActivationCodeByEmailAsync(string email)
    {
        using (var connection = Connect())
        {
            var result = await connection
                .QueryAsync<ActivationCode>("[qa].[Get_ActivationCodeByEmail]", new { Email = email }, 
                    commandTimeout: 120, commandType: CommandType.StoredProcedure);

            return result.SingleOrDefault();
        }
    }

I keep getting an error of

System.NullReferenceException: Object reference not set to an instance of an object.

on my line

 activationCodePage.ActivationCodeInput().SendKeys(activationCode.Code);

because, the Stored procedure pulled Null values, due to it being too fast. but when I step through it or add the thread sleep, it's enough time to get the required data from the database.

Any help would be greatly appreciated!

  • the method GetLatestActivationCodeByEmailAsync may not be waiting... include that code. (I don't think this is Selenium related, but rather that your await is not awaiting...) – pcalkins Nov 18 '20 at 01:14
  • @pcalkins - just added it above, for you to be able to see – ThatBraziliann Nov 18 '20 at 15:42
  • I think you want to await your "result.SingleOrDefault();" That's the one that runs/evaluates the query. As is, I think it's running async... – pcalkins Nov 18 '20 at 18:46
  • @pcalkins , thank you for your help, it lead us to a solution! Look down below. I appreciate it! – ThatBraziliann Nov 19 '20 at 15:10

1 Answers1

1

@pcalkins helped tremendously with realizing this.. The await was not actually waiting. When informed from my boss that its actually goes off and runs while the rest of my code continues - we came up with a nice solution.

 var activationCodeTask = ActivationCodeService.GetLatestActivationCodeByEmailAsync(deptEmail);

 var activationCode = await activationCodeTask;

This seems to solve the issue as it won't move on until the process and stored procedure is completed and results returned.