text Selenium UI测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Selenium UI测试相关的知识,希望对你有一定的参考价值。

1. create class library .net core project

2. install packages:
xunit, xunit.runner.visualstudio, selenium.webdriver, selenium.webdriver.ChromeDriver
Microsoft.extensions.PlatformAbstractions, DotNetSeleniumExtras.PageObjects.Core

3. Go to Debug > start without debuging

4. Unit test

public class CreditCardApplicationTests : IDisposable
{
    private readonly IWebDriver _driver;

    public CreditCardApplicationTests()
    {
        _driver = new ChromeDriver(GetContentRootPath());
    }

    [Fact]
    public void ShouldLoadApplicationPage_SmokeTest()
    {
        _driver.Navigate().GoToUrl("http://localhost:44108/apply");

        Assert.Equal("Credit Card Application - CreditCards", _driver.Title);
    }

    [Fact]
    public void ShouldValidateApplicationDetails()
    {
        _driver.Navigate().GoToUrl("http://localhost:44108/Apply");

        // Don't enter a first name

        IWebElement lastName = _driver.FindElement(By.Name("LastName"));
        lastName.SendKeys("Smith");

        DelayForDemoVideo();

        IWebElement frequentFlyerNum = _driver.FindElement(By.Id("FrequentFlyerNumber"));
        frequentFlyerNum.SendKeys("012345-A");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("Age")).SendKeys("18");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("GrossAnnualIncome")).SendKeys("100000");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("submitApplication")).Click();

        Assert.Equal("Credit Card Application - CreditCards", _driver.Title);

        IWebElement firstErrorMessage =
            _driver.FindElement(By.CssSelector(".validation-summary-errors ul > li"));

        Assert.Equal("Please provide a first name", firstErrorMessage.Text);
    }

    [Fact]
    public void ShouldDeclineLowIncomes()
    {
        _driver.Navigate().GoToUrl("http://localhost:44108/Apply");

        IWebElement firstName = _driver.FindElement(By.Name("FirstName"));
        firstName.SendKeys("Sarah");

        DelayForDemoVideo();

        IWebElement secondName = _driver.FindElement(By.Name("LastName"));
        secondName.SendKeys("Smith");

        DelayForDemoVideo();

        IWebElement frequentFlyerNum = _driver.FindElement(By.Id("FrequentFlyerNumber"));
        frequentFlyerNum.SendKeys("012345-A");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("Age")).SendKeys("35");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("GrossAnnualIncome")).SendKeys("10000");

        DelayForDemoVideo();

        _driver.FindElement(By.Id("submitApplication")).Click();


        Assert.Equal("Application Complete - CreditCards", _driver.Title);
        IWebElement applicationDecision = _driver.FindElement(By.Id("decision"));
        Assert.Equal("AutoDeclined", applicationDecision.Text);
    }

    /// <summary>
    /// Brief delay to slow down browser interactions for
    /// demo video recording purposes
    /// </summary>
    private static void DelayForDemoVideo()
    {
        Thread.Sleep(1000);
    }
    
    private string GetContentRootPath()
    {
        string testProjectPath = PlatformServices.Default.Application.ApplicationBasePath;

        var relativePathToWebProject = @"..\..\..\..\..\tests\CreditCards.UITests\bin\Debug\netcoreapp1.1";

        return Path.Combine(testProjectPath, relativePathToWebProject);
    }

    public void Dispose()
    {
        _driver.Quit();
        _driver.Dispose();
    }
}

##############Refactor:

ApplicationPage:

class ApplicationPage
{
    public IWebDriver Driver { get; }

    private const string PagePath = "apply";

    public ApplicationPage(IWebDriver driver)
    {
        Driver = driver;

        PageFactory.InitElements(driver, this);
    }

    public void NavigateTo()
    {
        var root = new Uri(Driver.Url).GetLeftPart(UriPartial.Authority);

        var url = $"{root}/{PagePath}";

        Driver.Navigate().GoToUrl(url);
    }

    [FindsBy(How = How.Name, Using = "FirstName")] // find by name
    private IWebElement FirstName { get; set; }


    [FindsBy(How = How.Name, Using = "LastName")]
    private IWebElement LastName { get; set; }


    [FindsBy(How = How.Id, Using = "FrequentFlyerNumber")]// find by id
    private IWebElement FrequentFlyerNumber { get; set; }


    [FindsBy(How = How.Id, Using = "Age")]
    private IWebElement Age { get; set; }


    [FindsBy(How = How.Id, Using = "GrossAnnualIncome")]
    private IWebElement GrossAnnualIncome { get; set; }


    [FindsBy(How = How.Id, Using = "submitApplication")]
    private IWebElement ApplyButton { get; set; }


    [FindsBy(How = How.CssSelector, Using = ".validation-summary-errors ul > li")]
    private IWebElement FirstError { get; set; }

    public string FirstErrorMessage => FirstError.Text;


    public void EnterName(string firstName, string lastName)
    {
        FirstName.SendKeys(firstName);
        LastName.SendKeys(lastName);
    }

    public void EnterFrequentFlyerNumber(string frequentFLyerNumber)
    {
        FrequentFlyerNumber.SendKeys(frequentFLyerNumber);
    }

    public void EnterAge(string age)
    {
        Age.SendKeys(age);
    }

    public void EnterGrossAnnualIncome(string income)
    {
        GrossAnnualIncome.SendKeys(income);
    }

    public ApplicationCompletePage SubmitApplication()
    {
        ApplyButton.Click();

        return new ApplicationCompletePage(Driver);
    }
}

ApplicationCompletePage:

internal class ApplicationCompletePage
{
    public IWebDriver Driver { get; }

    public ApplicationCompletePage(IWebDriver driver)
    {
        Driver = driver;

        PageFactory.InitElements(driver, this);
    }

    [FindsBy(How = How.Id, Using = "fullName")]
    private IWebElement Name { get; set; }

    [FindsBy(How = How.Id, Using = "decision")]
    private IWebElement Decision { get; set; }

    public string FullName => Name.Text;

    public string ApplicationDecision => Decision.Text;
}

Unit Test:

public class CreditCardApplicationTests : IDisposable
{
    private readonly IWebDriver _driver;
    private readonly ApplicationPage _applicationPage;

    public CreditCardApplicationTests()
    {
        _driver = new ChromeDriver(GetContentRootPath());

        _driver.Navigate().GoToUrl("http://localhost:44108/");

        _applicationPage = new ApplicationPage(_driver);
        _applicationPage.NavigateTo();
    }

    [Fact]
    public void ShouldLoadApplicationPage_SmokeTest()
    {
        Assert.Equal("Credit Card Application - CreditCards", _applicationPage.Driver.Title);
    }

    [Fact]
    public void ShouldValidateApplicationDetails()
    {
        // Don't enter a first name
        _applicationPage.EnterName("", "Smith");
        DelayForDemoVideo();

        _applicationPage.EnterFrequentFlyerNumber("012345-A");
        DelayForDemoVideo();

        _applicationPage.EnterAge("20");
        DelayForDemoVideo();

        _applicationPage.EnterGrossAnnualIncome("100000");
        DelayForDemoVideo();

        _applicationPage.SubmitApplication();

        Assert.Equal("Credit Card Application - CreditCards", _applicationPage.Driver.Title);

        Assert.Equal("Please provide a first name", _applicationPage.FirstErrorMessage);
    }

    [Fact]
    public void ShouldDeclineLowIncomes()
    {
        _applicationPage.EnterName("Sarah", "Smith");
        DelayForDemoVideo();

        _applicationPage.EnterFrequentFlyerNumber("012345-A");
        DelayForDemoVideo();

        _applicationPage.EnterAge("35");
        DelayForDemoVideo();

        _applicationPage.EnterGrossAnnualIncome("10000");
        DelayForDemoVideo();

        ApplicationCompletePage applicationCompletePage =
            _applicationPage.SubmitApplication();

        Assert.Equal("Application Complete - CreditCards", applicationCompletePage.Driver.Title);
        Assert.Equal("AutoDeclined", applicationCompletePage.ApplicationDecision);
    }

    /// <summary>
    /// Brief delay to slow down browser interactions for
    /// demo video recording purposes
    /// </summary>
    private static void DelayForDemoVideo()
    {
        Thread.Sleep(1000);
    }

    private string GetContentRootPath()
    {
        string testProjectPath = PlatformServices.Default.Application.ApplicationBasePath;

        var relativePathToWebProject = @"..\..\..\..\..\tests\CreditCards.UITests\bin\Debug\netcoreapp1.1";

        return Path.Combine(testProjectPath, relativePathToWebProject);
    }

    public void Dispose()
    {
        _driver.Quit();
        _driver.Dispose();
    }
}

以上是关于text Selenium UI测试的主要内容,如果未能解决你的问题,请参考以下文章

「UI 测试自动化selenium」汇总

Web UI自动化测试之Selenium3

UI 自动化测试平台解决方案使用 Selenium IDE 录制 UI 自动化测试脚本

UI 测试 - 断言排序数据 (Katalon/Selenium) Java

Selenium 对于 Web UI 测试就像 ________ 对于 Windows 应用程序 UI 测试 [关闭]

selenium测试框架快速搭建(ui自动化测试)