Below I’m debugging and throwing an exception on purpose to find out the value of a JavaScript call from WebDriver. How can I cast the jQuery call so I can print a string (based on the number of tr tags in my table with id of “viewtable”) in my exception message? I imagine this has absolutely nothing to do with the C# code. I bet the driver can’t execute the jQuery call properly, but I don’t know the correct syntax.
Exception thrown by NUnit:
Selenium.ProductPricing.TheUntitledTest:
System.InvalidCastException : Unable to cast object of type 'System.Int64' to type 'System.String'.
Environment:
- Class Library project is called Selenium.sln/Selenium.csproj
- project referencing NUnit dll & Selenium C# client drivers including WebDriver dll files
- project has one class class called ProductPricing.cs
- running class library dll in NUnit 2.6
Test Case C# code:
(search for “BAD!” below)
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;
}
}
}
}
Just from the exception I’m assuming that
ExecuteScriptis returning anint64for$("query").lengthand astringfor$("query").html().So you might want to try this:
or if you prefer a number:
Not sure about the second one but the first one should work.