Home > Software design >  Selenium c#- implementing wait functionality
Selenium c#- implementing wait functionality

Time:01-05

I have an assignment of writing Selenium C# code that login to a website and validating that the loading time was not more than 10 seconds. But the catch is that I am not allowed to use any waiting functionality. And of course I dont have URL because its a login process. I didnt find any answer of doing it without using wait. Can someone direct me or send me some focused tutorial ?

I wrote this but It wasnt acceptable

static WebDriver Login()
        {
            WebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl(URL);
            driver.FindElement(By.Id(ID_USERNAME)).SendKeys(USERNAME);
            driver.FindElement(By.Id(ID_PASSWORD)).SendKeys(PASSWORD);
            driver.FindElement(By.Id(ID_BUTTON_LOGIN)).Click();
            return driver;
        }
        static void CheckLoginTime(WebDriver driver)
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIME_TO_LOAD_SECONDS));
            long time1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            wait.Until(ExpectedConditions.ElementIsVisible(By.Id(ID_ACCOUNT_LINK)));
            long time2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            if (time2-time1 >= TIME_TO_LOAD_SECONDS*1000)
            {
                throw new Exception("time of login is over 10 seconds");
            }
        }

        static void CheckLogout(WebDriver driver)
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIME_TO_LOAD_SECONDS));
            wait.Until(ExpectedConditions.ElementIsVisible(By.Id(ID_ACCOUNT_LINK)));

            driver.FindElement(By.Id(ID_ACCOUNT_LINK)).Click();
            driver.FindElement(By.Id(ID_LOGOUT)).Click();
            driver.FindElement(By.Id(ID_OK_LOGOUT)).Click();

            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIME_TO_LOAD_SECONDS));
            wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(SELECTOR_HOVER)));
            string text = driver.FindElement(By.CssSelector(SELECTOR_HOVER)).GetAttribute("title");
            string path = Directory.GetCurrentDirectory();
            using (StreamWriter writer = new StreamWriter(path FILE_NAME))
            {
                writer.WriteLine(text);
            }
        }

        static void Main(string[] args)
        {
            WebDriver driver = Login();
            CheckLoginTime(driver);
            CheckLogout(driver);

        }

CodePudding user response:

So the answer was something like that: (simple "busy wait" loop)

static void CheckLoginTime(WebDriver driver)
        {
            long time1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            bool found = false;
            while (!found)
            {
                try
                {
                    driver.FindElement(By.Id(hashVarData["ID_ACCOUNT_LINK"]));
                }
                catch (Exception e)
                {
                    continue;
                }
                found = true;
            }
            long time2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            Console.WriteLine("time-> "   (time2 - time1));
            if (time2-time1 >= Int32.Parse(hashVarData["TIME_TO_LOAD_SECONDS"]) * 1000)
            {
                throw new Exception("time of login is over 10 seconds");
            }
        }

CodePudding user response:

You could use Navigation Timing API for timing related information. Hope this helps.

  •  Tags:  
  • Related