在Visual Studio

时间:2017-01-23 10:02:47

标签: selenium testing

我在Visual Studio 2013中运行测试(从Selenium IDE导出)时遇到了麻烦。

当我使用Selenium IDE时,所有测试都没有任何问题。

将我们导出为 .cs 文件后,Visual Studio会毫无困难地发现它们,但无法让它们通过基本URL工作。换句话说,它加载页面,但没有任何反应。过了一会儿,它进入超时并退出浏览器。

是否需要编辑某些代码以使其正常工作?

我添加了命令1.等待,2。验证和3. 点击按钮/元素/电台/链接,但它没有帮助。

我希望你能帮助我以某种方式解决这个问题。此致!

代码(没有基本网址):

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class Black
    {
        private IWebDriver driver;
    private StringBuilder verificationErrors;
    private string baseURL;
    private bool acceptNextAlert = true;

    [SetUp]
    public void SetupTest()
    {
        driver = new FirefoxDriver();
        baseURL = "https://something.com/";
        verificationErrors = new StringBuilder();
    }

    [TearDown]
    public void TeardownTest()
    {
        try
        {
            driver.Quit();
        }
        catch (Exception)
        {
            // Ignore errors if unable to close the browser
        }
        Assert.AreEqual("", verificationErrors.ToString());
    }

    [Test]
    public void TheBlackTest()
    {
        driver.Navigate().GoToUrl(baseURL + "something");
        for (int second = 0; ; second++)
        {
            if (second >= 60) Assert.Fail("timeout");
            try
            {
                if (IsElementPresent(By.Id("something"))) break;
            }
            catch (Exception)
            { }
            Thread.Sleep(1000);
        }

        for (int second = 0; ; second++)
        {
            if (second >= 60) Assert.Fail("timeout");
            try
            {
                if (IsElementPresent(By.Id("something"))) break;
            }
            catch (Exception)
            { }
            Thread.Sleep(1000);
        }
        try
        {
            Assert.IsTrue(IsElementPresent(By.Id("something")));
        }
        catch (AssertionException e)
        {
            verificationErrors.Append(e.Message);
        }
        driver.FindElement(By.CssSelector("#something > div.ng-binding")).Click();
        driver.FindElement(By.CssSelector("#something > div.ng-binding")).Click();
        driver.FindElement(By.Id("something")).Click();
        driver.FindElement(By.Id("something")).Click();
    }
    private bool IsElementPresent(By by)
    {
        try
        {
            driver.FindElement(by);
            return true;
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }

    private bool IsAlertPresent()
    {
        try
        {
            driver.SwitchTo().Alert();
            return true;
        }
        catch (NoAlertPresentException)
        {
            return false;
        }
    }

    private string CloseAlertAndGetItsText()
    {
        try
        {
            IAlert alert = driver.SwitchTo().Alert();
            string alertText = alert.Text;
            if (acceptNextAlert)
            {
                alert.Accept();
            }
            else
            {
                alert.Dismiss();
            }
            return alertText;
        }
        finally
        {
            acceptNextAlert = true;
        }
    }
}

0 个答案:

没有答案