FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Lesson learnt! Be more attention to details #6

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

Introduction

Yesterday, I was implementing what I learned the day before which was about ExplicitWait Method. Previously, I used the "Thread Sleep Method" to force wait a certain time.

I copied the code from the tutor code like this:

 public void Test1_FixedExplicitly()
        {
            _driver.Navigate().GoToUrl(URL.SlowAnimationUrl);
            FillOutCreditCardInfo();
            var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
            wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("go"))).Click();
            Assert.IsTrue(wait.Until(ExpectedConditions.ElementIsVisible(By.Id("success"))).Displayed);
        }

My code was firstly simply modified to be like this:

driver.Navigate().GoToUrl(URL.NewInvoiceUrl);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(6));
            Assert.IsTrue(wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@type='submit']"))). Click();

Problem I encountered

I knew the last line of code was to verify if the element has been available to be clickable, but it's not only that, It was supposed to click it too. Then the problem was what I was testing-- the accounting software was happened not to work properly concerning to this button. It has caused me some trouble. Because of the button was not able to click, the element which was to be located was not able to display. It was really annoying. Because the code without an explicit wait method was able to run successfully. Then I looked into my code line by line and debugged. Finally, I was able to figure out this was the reason. so I changed into this (I only need to check if it is available, That's it!)

driver.Navigate().GoToUrl(URL.NewInvoiceUrl);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(6));
            Assert.IsTrue(wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@type='submit']"))).Displayed);

Then it works well.

So I think it is all because I was not paying too much attention to what I really needed and I was in a rush so It gave me a lesson. Now I leaned every time I should know how the code fits in my codes. What I need to adjust for my own code if where is necessary.

Thanks for the video on my! Really helpful for me. Every time I solved a problem by myself I would be very happy because I leanrt something new and I realise where I need to improve. This is what is called-- great sense of accomplishment! Now I need to move on to PageObject.

Thanks.