无法运行类中包含的自动化测试

时间:2019-03-31 08:06:46

标签: c# selenium-webdriver mstest

我是硒的新手,我似乎无法运行包含在不同方法和类中的测试。

在将方法添加到类中之前,我可以运行良好的测试。但是作为班级分配,我们需要将测试分为不同的班级。

非常感谢您的帮助!

以下是我使用的不同方法和类:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using System.Threading;

namespace firstProject
{
[TestClass]
class automation
{
    IWebDriver driver = new ChromeDriver();

    [TestMethod]
    public void PurchaseIFrame()
    {
        driver.Url = "http://automationpractice.com/index.php";
        var homefeatured = driver.FindElement(By.Id("homefeatured"));
        var items = homefeatured.FindElements(By.TagName("li"));
        items[0].FindElement(By.Id("icon-eye-open")).Click();
    }

    [TestMethod]
    public void PurchaseBlouse()
    {
        driver.Url = "http://automationpractice.com/index.php";
        IWebElement login = driver.FindElement(By.ClassName("login"));
        login.Click();

        IWebElement email = driver.FindElement(By.Id("email"));
        email.SendKeys("tester@test.com");

        IWebElement password = driver.FindElement(By.Id("passwd"));
        password.SendKeys("12345");

        IWebElement submitLogin = driver.FindElement(By.Id("SubmitLogin"));
        submitLogin.Click();

        IWebElement homearea = driver.FindElement(By.ClassName("footer_links"));
        IWebElement homeBtn = homearea.FindElement(By.ClassName("btn"));
        homeBtn.Click();

        IWebElement catArea = driver.FindElement(By.Id("block_top_menu"));
        IWebElement women = catArea.FindElement(By.ClassName("sf-with-ul"));
        women.Click();

        var results = driver.FindElements(By.ClassName("ajax_block_product"));
        var blouse = results[1];
        IWebElement img = blouse.FindElement(By.ClassName("img-responsive"));
        img.Click();

        IWebElement addToCart = driver.FindElement(By.Id("add_to_cart"));
        addToCart.Click();

        Thread.Sleep(4000);

        IWebElement layerCart = driver.FindElement(By.ClassName("layer_cart_cart"));
        IWebElement buttonContainer = layerCart.FindElement(By.ClassName("button-container"));
        IWebElement buttonPro = buttonContainer.FindElement(By.ClassName("button-medium"));
        IWebElement clickBut = buttonPro.FindElement(By.TagName("span"));
        clickBut.Click();

        IWebElement proCheck = driver.FindElement(By.ClassName("cart_navigation"));
        IWebElement proBtn = proCheck.FindElement(By.ClassName("button-medium"));
        IWebElement clickCheck = proBtn.FindElement(By.TagName("span"));
        clickCheck.Click();

        IWebElement submitAddress = driver.FindElement(By.Name("processAddress"));
        submitAddress.Click();

        IWebElement mark = driver.FindElement(By.Id("cgv"));
        mark.Click();

        IWebElement submitProceed = driver.FindElement(By.Name("processCarrier"));
        submitProceed.Click();

        IWebElement bankWire = driver.FindElement(By.ClassName("bankwire"));
        bankWire.Click();

        IWebElement a = driver.FindElement(By.ClassName("cart_navigation"));
        IWebElement b = a.FindElement(By.ClassName("button-medium"));
        IWebElement c = b.FindElement(By.TagName("span"));
        c.Click();

        driver.Quit();


      }
  }
}

[TestClass]
public class AutomationClass
{
    public IWebDriver driver = new ChromeDriver();
    public Main main;

    [TestInitialize]
    public void before()
    {
        driver.Url = "http://automationpractice.com/index.php";
        main = new Main(driver);
    }
}

    public class CartProcess
{
    public Checkout prceedToCheckout()
    {
        throw new NotImplementedException();
    }
}

public class Checkout
{
}

public class Dress
{
    private IWebDriver _driver;

    public Dress(IWebDriver driver)
    {
        _driver = driver;
    }

    public CartProcess AddToCart()
    {
        throw new NotImplementedException();
    }
}

public class Main
{

    private IWebDriver _driver;

    private IWebElement search_query_top => _driver.FindElement(By.Id("search_query_top"));
    private IWebElement submit_search => _driver.FindElement(By.Name("submit_search"));
    public Main(IWebDriver driver)
    {
        _driver = driver;
    }

    public  Result Search(string dressName)
    {
        search_query_top.SendKeys(dressName);
        submit_search.Click();
        return new Result(_driver);
    }


}

class number
{
    public int i;
    public number(int number)
    {
        i = number;
    }
}

[TestClass]
public class PageobjectTest :AutomationClass
{
    [TestMethod]
    public void TestMethod1()
    {
       Result result = main.Search("summer");
       Dress dress =  result.select(2);
       CartProcess cartprocess = dress.AddToCart();
       Checkout checkout = cartprocess.prceedToCheckout();


    }
}

public class Result
{
    private IWebDriver _driver;
    private IWebElement product_list => _driver.FindElement(By.ClassName("product_list"));
    private List<IWebElement> dress => product_list.FindElements(By.TagName("li")).ToList();
    public Result(IWebDriver driver)
    {
        _driver = driver;
    }

    public Dress select(int v)
    {
        dress[v].FindElement(By.ClassName("product-image-container")).Click();
        return new Dress(_driver);

    }
}

0 个答案:

没有答案