两个测试在一起跑但不能独立通过时不会通过

时间:2017-03-16 18:17:38

标签: javascript c# selenium-webdriver nunit

问题:

我独立运行时每个传递两个测试用例但是当一起运行时,第一个运行将通过而第二个运行将失败。更有趣的是,如果我先切换哪个测试(NUnit按字母顺序运行,那么在测试方法名称中添加'A'就可以了)?

以下是一些可以重现我的问题的代码。它显然与JavaScript操作有关,但我不知道为什么。

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;

namespace POMAuctivaTest.TestSuite
{
    [TestFixture]
    [Category("test")]
    public class Test
    {
        public IWebDriver driver { get; set; }
        string listingUrl = "http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&item=110194475127#ht_1235wt_1139";
        [OneTimeSetUp]
        public void setUp()
        {
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();
        }

        [Test]
        public void test_CounterTest()
        {
            driver.Navigate().GoToUrl(listingUrl);
            WaitForElementVisible(By.CssSelector("#but_v4-28binLnk"));  //  Buy it now button selector
            Assert.IsTrue(IsElementPresent(By.CssSelector("#ngvi_desc_div > div > div > div.aucCounter")), "Counter was not found on page");
        }

        [Test]
        public void test_ScrollingGallery()
        {
            var listingTitleUsed = "Test Listing Do Not Bid Or Buy";
            driver.Navigate().GoToUrl(listingUrl);
            WaitForElementVisible(By.CssSelector("#but_v4-28binLnk"));  //  Buy it now button selector
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            var script = "document.getElementById('csgGallery').style.display='block';";
            js.ExecuteScript(script);
            WaitForElementVisible(By.CssSelector("div[class=csgTitle]"));
            var listingName = getSudoElementContent("div[class=csgTitle]", ":after");
            listingName = listingName.Replace("/", "");
            listingName = listingName.Replace("\"", "");
            Assert.AreEqual(listingTitleUsed, listingName);
        }

        [OneTimeTearDown]
        public void tearDown()
        {
            driver.Quit();
        }

        public string getSudoElementContent(string selector, string cssEvent)
        {
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            string script = String.Format("return window.getComputedStyle(document.querySelector('{0}'),'{1}').getPropertyValue('content')", selector, cssEvent);
            string content = (String)js.ExecuteScript(script);
            return content;
        }

        public void WaitForElementVisible(By element)
        {
            var attempts = 0;
            while (attempts < 2)
            {
                try
                {
                    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
                    wait.Until(ExpectedConditions.ElementIsVisible(element));
                    break;
                }
                catch (WebDriverException)
                {
                    attempts++;
                }
                catch (InvalidOperationException)
                {
                    attempts++;
                }
            }
            if (attempts >= 2)
            {
                throw new NoSuchElementException("Cannot locate " + element.ToString());
            }
        }

        public bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (WebDriverTimeoutException)
            {
                return false;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
            catch (WebDriverException)
            {
                return false;
            }
        }
    }
}

PS当我在夜间测试中测试此功能时,我会为每个测试用例使用新的列表。

当第二次运行test_ScrollingGallery时,我得到此异常

Test Name:  test_ScrollingGallery
Test FullName:  POMAuctivaTest.TestSuite.Test.test_ScrollingGallery
Test Source:    C:\git\auctiva-webdriver\POMAuctivaTest.TestSuite\Test.cs : line 32
Test Outcome:   Failed
Test Duration:  0:00:02.311

Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
   at POMAuctivaTest.TestSuite.Test.test_ScrollingGallery() in C:\git\auctiva-webdriver\POMAuctivaTest.TestSuite\Test.cs:line 38
Result Message: 
System.InvalidOperationException : unknown error: Cannot read property 'style' of null
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64)

当我运行test_Countertest秒时,我收到此错误

Test Name:  test_CounterTest
Test FullName:  POMAuctivaTest.TestSuite.Test.test_CounterTest
Test Source:    C:\git\auctiva-webdriver\POMAuctivaTest.TestSuite\Test.cs : line 24
Test Outcome:   Failed
Test Duration:  0:00:02.34

Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByCssSelector(String cssSelector)
   at OpenQA.Selenium.By.<>c__DisplayClass1e.<CssSelector>b__1c(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at POMAuctivaTest.TestSuite.Test.test_CounterTest() in C:\git\auctiva-webdriver\POMAuctivaTest.TestSuite\Test.cs:line 27
Result Message: 
OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"css selector","selector":"#ngvi_desc_div > div > div > div.aucCounter"}
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64)

0 个答案:

没有答案
相关问题