简体   繁体   中英

How to fix test automation code using Selenium with C#?

I am working on automated tests for the first time using Selenium with C#. I am following some instructions as a beginner from this link . However, it is not working. It says 1 Test failed. I have the following code:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


namespace OnlineStore.TestCases
{
    class LogInTest
    {
        [Test]
        public void Test()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "http://www.store.demoqa.com";

            // Find the element that's ID attribute is 'account'(My Account) 
            driver.FindElement(By.XPath(".//*[@id='account']/a")).Click();

            // Find the element that's ID attribute is 'log' (Username)
            // Enter Username on the element found by above desc.
            driver.FindElement(By.Id("log")).SendKeys("testuser_1");

            // Find the element that's ID attribute is 'pwd' (Password)
            // Enter Password on the element found by the above desc.
            driver.FindElement(By.Id("pwd")).SendKeys("Test@123");

            // Now submit the form.
            driver.FindElement(By.Id("login")).Click();

            // Find the element that's ID attribute is 'account_logout' (Log Out)
            driver.FindElement(By.XPath(".//*[@id='account_logout']/a")).Click();

            // Close the driver
            driver.Quit();

        }
    }
}

And the following message:

[10/6/2019 5:05:53 AM Informational] Executing test method 'OnlineStore.TestCases.LogInTest.Test'
[10/6/2019 5:05:53 AM Informational] ------ Run test started ------
[10/6/2019 5:05:54 AM Informational] NUnit Adapter 3.15.1.0: Test execution started
[10/6/2019 5:05:54 AM Informational] Running selected tests in C:\Users\enead\source\repos\OnlineStore\OnlineStore\bin\Debug\OnlineStore.dll
[10/6/2019 5:05:55 AM Informational]    NUnit3TestExecutor converted 1 of 1 NUnit test cases
[10/6/2019 5:05:55 AM Informational] NUnit Adapter 3.15.1.0: Test execution complete
[10/6/2019 5:05:55 AM Informational] ========== Run test finished: 1 run (0:00:01.8817664) ==========

I've searched multiple websites to find an answer but without success. What is wrong? What can I do?
Edits
I am providing some screenshots.

1 测试失败

错误信息

指定驱动程序位置后出错

Looking at the Error Message screenshot, you need to specify location of chromedriver.exe while initializing driver in your Test.

IWebDriver driver = new ChromeDriver("location_of_chromedriver.exe");
  1. There is a banner at the top of this page that is blocking the 'account' element. 在此处输入图像描述 You need to add a test step where you click to 'dismiss' this banner first.

driver.FindElement(By.LinkText("Dismiss").Click();

  1. The script in Visual Studio will always move faster than the browser so you need to add steps in the script where you wait for the page to load before you click on new elements.

An easy way to do this is using a static wait method like this:

Task.Delay(2000).Wait();

You will also need to add: using System.Threading.Tasks;

The '2000' is the amount of milliseconds you want to wait.

A more dynamic way of waiting is first creating a wait method and then calling on that method whenever you want to wait on something specific to happen (in this case, waiting for the account link to be clickable).

Creating a dynamic wait method and using it looks like this:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("By.LinkText("My Account"))).Click();

For this method you also need: using OpenQA.Selenium.Support.UI;

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