Hi all,
I was looking at my code from Day 21 of the 30 Days of Tools challenge. This involved clicking the button on the following website: Click the Button - Ministry of Testing
In my code, I used GetAttribute(“src”) to check if the src attribute contained the words red or green to indicate the current colour of the button. Because it took about 5 seconds for the button to change back from red to green, I added a 5 second wait until the test checked the colour again.
Assert.True(button.GetAttribute("src").Contains("green"));
button.Click();
Assert.True(button.GetAttribute("src").Contains("red"));
Thread.Sleep(4000);
Assert.True(button.GetAttribute("src").Contains("green"));
It was pointed out to me that this was inefficient so I was looking to see if I could adjust the code so it waited until the colour changed rather than just waited 5 seconds. I’m struggling to find a way to make it so the test waits for an attribute to change. Just wondering if anyone has any ideas how to wait for an element attribute to change to a certain value?
The best I’ve come up with so far is to have 2 separate elements, 1 for the button when its red and another for the button when its green and then wait for the green button to exist.
[OneTimeSetUp]
public void Setup()
{
Driver = new ChromeDriver(Environment.CurrentDirectory);
Driver.Navigate().GoToUrl("https://ministryoftesting.github.io/the-button/#");
Driver.Manage().Window.Maximize();
}
[OneTimeTearDown]
public void Teardown()
{
Driver.Quit();
}
[TestCase]
public void CheckButtonWorksAsExpected()
{
IWebElement button = Driver.FindElement(By.Id("buttonImage"));
By greenButton = By.XPath("//img[contains(@src, 'green')]");
By redButton = By.XPath("//img[contains(@src, 'red')]");
Assert.True(Driver.FindElement(greenButton).Displayed);
button.Click();
Assert.True(Driver.FindElement(redButton).Displayed);
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(greenButton));
}