2017-02-23 115 views
1

我完全新的这场比赛,虽然已经手动测试了一段时间,我决定尝试我的手在自动化:)期望的字符串长度错误

我已经记录在硒IDE的脚本,并转换为C# 。该脚本将创建一个帐户,添加一张虚拟测试卡并将现金存入该帐户。

我还不是很了解C#;我正在通过YouTube教程学习基础知识。如果你能回答它,将不胜感激,当裸考虑到这一点,每个人必须学会的地方:)

试图运行单元测试脚本时,我收到以下错误。

Test Name:  TheVisaDebitRegistrationTest 
Test FullName: SeleniumTests.VisaDebitRegistration.TheVisaDebitRegistrationTest 
Test Source: c:\users\lee.davies\documents\visual studio 2015\Projects\UnitTestProject6\UnitTestProject6\UnitTest1.cs : line 44 
Test Outcome: Failed 
Test Duration: 0:00:14.862 

Result StackTrace: at SeleniumTests.VisaDebitRegistration.TheVisaDebitRegistrationTest() in c:\users\lee.davies\documents\visual studio 2015\Projects\UnitTestProject6\UnitTestProject6\UnitTest1.cs:line 52 
Result Message: 
Expected string length 55 but was 76. Strings differ at index 0. 
    Expected: "Registration | Sign up to Betfred.com | £25 Matched Bet" 
    But was: "Betfred online betting, sports, casino, games, poker and Bing..." 
    -----------^ 

[Test] 
public void TheVisaDebitRegistrationTest() 
{ 
    // open |/| 
    driver.Navigate().GoToUrl(baseURL + "/"); 
    // assertTitle | Betfred online betting, sports, casino, games, poker and Bingo | Betfred.com | 
    Assert.AreEqual("Betfred online betting, sports, casino, games, poker and Bingo | Betfred.com", driver.Title); 
    // click | link=Sign Up Now | 
    driver.FindElement(By.LinkText("Sign Up Now")).Click(); 
    // assertTitle | Registration | Sign up to Betfred.com | £25 Matched Bet | 
    Assert.AreEqual("Registration | Sign up to Betfred.com | £25 Matched Bet", driver.Title); 
+0

大概您没有使用预期标题的页面网址。 –

+0

请看看[how-to-ask](http://stackoverflow.com/help/how-to-ask) – swe

回答

1

测试失败在第二Assert.AreEqualAssert.AreEqual("Registration | Sign up to Betfred.com | £25 Matched Bet", driver.Title);。该错误消息告诉您究竟为什么

预计:“注册|登录到Betfred.com |£25个符合条件的赌注”
不过是:“Betfred网上投注,体育,娱乐,游戏,扑克和宾果| Betfred.com”

driver.Title仍然"Betfred online betting, sports, casino, games, poker and Bingo | Betfred.com"

0

它说,你把在Assert.AreEqual期望的字符串是不一样的实际之一。你可以做的事情是调试测试,看看实际的字符串,并检查是否这是一个错误或没有。

string actualResult = driver.Title; 
Debug.WriteLine(driver.Title); 

如果您将这些行添加到您的代码中,而不是运行单击调试,则实际的整个标题将显示在输出窗口中。或者,您可以添加断点并悬停标题属性。

1

你应该等到登出动作完成。

所以,把wait

driver.FindElement(By.LinkText("Sign Up Now")).Click(); 

一个可能的解决方案后,等待是:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.TitleIs("Registration | Sign up to Betfred.com | £25 Matched Bet")) 
0

的问题是,第二Assert失败,因为标题不匹配预期。该ExpectedBut was告诉你什么是被比较,并在那里眼福(第一个字符)。

你碰到的问题是,硒不会等待页面加载(通常情况下),当您使用.GoToUrl()除外。还有就是为什么你可以看起了长长的解释,但它基本上是因为硒不知道你把网页上的哪个动作将触发页面加载,所以它留给用户采取等待照顾。

在这种情况下,第二个Assert在页面转换发生之前执行,因此您将注册页面标题(第2页)与当前页面(第1页)标题进行比较,该标题失败。为了解决这个问题,你想等待注册页面加载。有许多方法可以做到这一点。您可以等待页面标题改变

string signUpPageTitle = "Registration | Sign up to Betfred.com | £25 Matched Bet"; 
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.TitleIs(signUpPageTitle)); 
Assert.AreEqual(signUpPageTitle, Driver.Title,); 

或者您可以等待注册页面上的唯一元素出现。

string signUpPageTitle = "Registration | Sign up to Betfred.com | £25 Matched Bet"; 
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10)); 
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("someId"))); 
Assert.AreEqual(signUpPageTitle, Driver.Title, "Validate title on sign up page"); 

一个抛开......我建议你添加了评论Assert s到表明您正在测试的内容。它将帮助您稍后进行调试等。例如,

Assert.AreEqual("Registration | Sign up to Betfred.com | £25 Matched Bet", driver.Title, "Validate title on sign up page"); 
+0

非常感谢你JeffC,这个工作很好吃:) –

+0

如果你发现这个(或任何)答案有帮助,请upvote它。如果这回答了您的问题,请将其标记为已接受的答案。谢谢! – JeffC