【发布时间】:2012-01-17 22:37:57
【问题描述】:
下面我正在调试并故意抛出异常以找出来自 WebDriver 的 JavaScript 调用的值。如何转换 jQuery 调用,以便在我的异常消息中打印一个字符串(基于我的表中 tr 标签的数量,id 为“viewtable”)?我想这与 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# 代码:
(在下面搜索“糟糕!”)
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;
}
}
}
}
【问题讨论】:
-
您遇到的问题与您的 C# 代码一切有关。 Ints 不能直接转换为字符串,
IJavaScriptExecutor.ExecuteScript()返回一个object强制转换为正确的类型。在这种情况下,来自 jQuery 选择器的.length属性返回一个 JavaScript 数字类型,因此 C# 语言绑定从ExecuteScript()调用返回一个 int。
标签: c# jquery .net webdriver selenium-webdriver