简体   繁体   中英

How to handle an error on a webpage selenium c#

I've written code that goes onto https://energy.gocompare.com/gas-electricity .

I want to introduce another parameter in my code. So when i go onto the webpage and click the continue button without entering any data i get an validation error on the page 'Please provide your postcode, as different regions have different fuel prices.'

How can i introduce a check in my code so that it validates this message displayed is correct. I've tried to find an XPath but i'm not sure that is the right way to go about it

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class RunPath
    {

            static void Main()
            {

                IWebDriver webDriver = new ChromeDriver();
                webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
                webDriver.Manage().Window.Maximize();

webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

i tried this cssSelector

     div[class$='validation-error']

Looking fetching error message.

In Java+TestNG, usually will do assertions

  Assert.assertEquals(driver.findElement(By.cssSelector("div[class$='validation-error']")).getText().trim(), "Please provide your postcode, as different regions have different fuel prices.");

you need to wait for validation error element:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class RunPath
    {
            static void Main()
            {
                IWebDriver webDriver = new ChromeDriver();
                webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
                webDriver.Manage().Window.Maximize();

                webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

                WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
                IWebElement error = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
                Assert.AreEqual("Please provide your postcode, as different regions have different fuel prices.", validationError.Text.TextTrim());
            }
    }           
}   

After clicking on continue button, you can try this code :

WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement error = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
Assert.AreEqual("Please provide your postcode, as different regions have different fuel prices.", error.Text.TextTrim());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM