无法在Selenium 2.0 Web驱动程序NUnit测试用例中强制转换为System.String

本文关键字:转换 String System 测试用例 NUnit Selenium 驱动程序 Web | 更新日期: 2023-09-27 18:20:40

下面我正在调试并故意抛出一个异常,以找出WebDriver的JavaScript调用的值。如何强制执行jQuery调用,以便在异常消息中打印字符串(基于id为"viewtable"的表中tr标记的数量)?我想这与C#代码完全无关。我打赌驱动程序无法正确执行jQuery调用,但我不知道正确的语法。

NUnit引发异常:

Selenium.ProductPricing.TheUntitledTest:
System.InvalidCastException : Unable to cast object of type 'System.Int64' to type 'System.String'.

环境:

  • 类库项目名为Selenium.sln/Selenium.csproj
  • 引用NUnit dll的项目&Selenium C#客户端驱动程序,包括WebDriver dll文件
  • 该项目有一个名为ProductPricing.cs的类
  • 在NUnit 2.6中运行类库dll

测试用例C#代码:
(在下面搜索"BAD!")

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using Selenium;
using System.Text;
using System;
namespace Selenium
{
    [TestFixture]
    public class ProductPricing
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        [SetUp]
        public void Setup()
        {
            driver = new FirefoxDriver();
            baseURL = "http://buyemp.qa.xxx.com/";
            ISelenium selenium = new WebDriverBackedSelenium(driver, baseURL);
            selenium.Start();
            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 TheUntitledTest()
        {
            //String var_skip_product = "false";
            String var_admin_user = "coders@xxx.com";
            String var_admin_pass = "notsure";
            driver.Navigate().GoToUrl(baseURL + "/admin");
            driver.FindElement(By.Id("email")).Clear();
            driver.FindElement(By.Id("email")).SendKeys(var_admin_user);
            driver.FindElement(By.Id("password")).Clear(); 
            driver.FindElement(By.Id("password")).SendKeys(var_admin_pass);
            driver.FindElement(By.CssSelector("input[type='"submit'"]")).Click();            
            driver.WaitForElement(By.LinkText("Products"));
            driver.FindElement(By.LinkText("Products")).Click();
            String var_product_row = "24";  // force script to start on row 24/25
            //// ERROR: Caught exception [unknown command [getTableTrCount]]
            // Command: getTableTrCount | Target: viewtable | Value: var_table_row_count (user extensions don't work in WebDriver)
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
            // this one throws an exception with value 22 - GOOD!
            //int x = Convert.ToInt32((string)js.ExecuteScript("return '22'"));
            // this one throws an exception with the cast exception - BAD!
            int x = Convert.ToInt32((string)js.ExecuteScript("return $('#viewtable tr').length"));
            // explicitly throwing Selenium exception so we can debug this code in NUnit
            throw new SeleniumException(x.ToString());
            // Command: storeText | Target: //a[@title='last page']/text() | Value: var_page_total_text
            // Conversion: String var_page_total_text = driver.FindElement(By.XPath("//a[@title='last page']/text()")).Text;
            String var_page_total_text = driver.FindElement(By.XPath("//a[@title='last page']")).Text;
            //// ERROR: Caught exception [ERROR: Unsupported command [getEval]]
            // Command: eval | Target: javascript{storedVars['var_page_total_text'].substring(1,storedVars['var_page_total_text'].length-1)}
            //int var_page_total = Convert.ToInt32(var_page_total_text.Substring(1,var_page_total_text.Length-1));           
        }    
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
    }
}

无法在Selenium 2.0 Web驱动程序NUnit测试用例中强制转换为System.String

从异常开始,我假设ExecuteScript$("query").length返回int64,为$("query").html()返回string

所以你可能想试试这个:

string x = js.ExecuteScript("return $('#viewtable tr').length").ToString();

或者如果你喜欢一个号码:

long x = (long)js.ExecuteScript("return $('#viewtable tr').length");

不确定第二个,但第一个应该有效。

这似乎是一个错误,或者Selenium不喜欢选择器的分配。如果你有什么想法,请告诉我。除非我在选择器中用于附加tr的语法在本网站的jQuery版本中不受支持。

尽管事实并非如此,因为下面的SeleniumIDE用户扩展自定义命令运行良好。

function jQuery(selector)
{
    return selenium.browserbot.getUserWindow().jQuery(selector);
}
Selenium.prototype.doGetTableTrCount = function(tableName, varStore) { 
    this.doStore(jQuery('#' + tableName + ' tr').length,varStore);
};

这是有效的:

IWebElement webElement = (RemoteWebElement)js.ExecuteScript("return $('#viewtable').get(0);");
string jQuerySelector = "arguments[0]";
string x = (string)js.ExecuteScript("return $(" + jQuerySelector + ").html()", webElement);
throw new SeleniumException(x);

这是有效的:

string x = (string)js.ExecuteScript("return $('#viewtable').html()");
throw new SeleniumException(x);

这不起作用:

IWebElement webElement = (RemoteWebElement)js.ExecuteScript("return $('#viewtable tr').get(0);");
string jQuerySelector = "arguments[0]";
string x = (string)js.ExecuteScript("return $(" + jQuerySelector + ").length", webElement);
throw new SeleniumException(x);

这不起作用:

string x = (string)js.ExecuteScript("return $('#viewtable tr').length");
throw new SeleniumException(x);
相关文章: