Lee Davies
Lee Davies

Reputation: 21

Expected String Length Error

I am completely new to this game, having been a manual tester for some time i have decided to try my hand at automation :)

I have recorded a script in Selenium IDE and converted to C#. The script is to create an account, add a dummy test card and deposit cash to the account.

I know very little about C#; I am learning the basics via youtube tutorials. If you could bare that in mind when answering it would be greatly appreciated, everyone's gotta learn somewhere :)

I am getting the following error when trying to run a unit test script.

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);

Upvotes: 1

Views: 4344

Answers (4)

JeffC
JeffC

Reputation: 25610

The issue is that the 2nd Assert is failing because the title doesn't match what was expected. The Expected and But was is telling you what was being compared and where it failed to match (the first character).

The problem you are running into is that Selenium will not wait for the page to load (generally) except when you are using .GoToUrl(). There's a long explanation of why that you can look up but it's basically because Selenium doesn't know what actions you take on the page will trigger a page load so it's left to the user to take care of waiting.

In this case, the 2nd Assert is executed before the page transition happens so you are comparing the Sign Up page title (2nd page) to the current page (1st page) title, which fails. To solve this, you want to wait for the Sign Up page to load. There are any number of ways to do this. You could wait for the page title to change

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, );

or you could wait for a unique element on the Sign Up page to be present.

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");

One aside... I would suggest that you add a comment to the Asserts to indicate what you are testing. It will help you debug things later, etc. For example

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

Upvotes: 0

Linh Nguyen
Linh Nguyen

Reputation: 1138

You should wait until the sign out action finishes.

So, put a wait after

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

One possible solution to wait is:

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

Upvotes: 1

Anton Angelov
Anton Angelov

Reputation: 1713

It says that the expected string that you put in Assert.AreEqual is not the same as the actual one. The thing you can do is to debug your test and see the actual string and check if this is a bug or not.

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

If you add these lines to your code and instead of run click debug the actual whole title will be displayed in the output window. Alternatively, you can add break-point and hover the title property.

Upvotes: 0

Guy
Guy

Reputation: 50899

The tests fails on the second Assert.AreEqual, Assert.AreEqual("Registration | Sign up to Betfred.com | £25 Matched Bet", driver.Title);. The error message tells you exactly why

Expected: "Registration | Sign up to Betfred.com | £25 Matched Bet"
But was: "Betfred online betting, sports, casino, games, poker and Bingo | Betfred.com"

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

Upvotes: 1

Related Questions