Parkyster
Parkyster

Reputation: 155

How to pass username and email in Selenium

I am using C#, Selenium with the page object model.

I need to understand how to pass a username and password from my test code, into my page object.

Example of my current test below:

[TestMethod]
public void Registeruser()
{
     Registrationpage.GoTo();
     Registrationpage.Enterusername();
     Registrationpage.EnterEmail();
}

Extract from Registration page (page object)

public static void Enterusername()
{
    Driver.Instance.FindElement(By.Id("MainContent_RegisterUser_CreateUserStepContainer_UserName")).SendKeys("testusername");
}

public static void Email()
{
    Driver.Instance.FindElement(By.Id("MainContent_RegisterUser_CreateUserStepContainer_Email")).SendKeys("testemail");
}

As you can see, at present i have my entrys in the sendkeys of my pageobject.

Instead, I want to be able to specify the testusername and testemail entrys in my test code so i can vary the text entered on each test.

I understand i need to specify these as strings, but i dont quite understand how to go about this in the pageobject code,I hope this makes sense, any help appreciated ( below is what I want to do)

[TestMethod]
public void Registeruser()
{
     Registrationpage.GoTo();
     Registrationpage.Enterusername(testusername);
     Registrationpage.EnterEmail(testemail);
}

Upvotes: 0

Views: 329

Answers (1)

Yi Zeng
Yi Zeng

Reputation: 32855

You might want to change your methods to take in string parameters.

public static void Enterusername(string testusername)
{
    Driver.Instance.FindElement(By.Id("MainContent_RegisterUser_CreateUserStepContainer_UserName")).SendKeys(testusername);
}

public static void EnterEmail(string testemail)
{
    Driver.Instance.FindElement(By.Id("MainContent_RegisterUser_CreateUserStepContainer_Email")).SendKeys(testemail);
}

Then call the methods with strings:

[TestMethod]
public void Registeruser()
{
    Registrationpage.GoTo();
    Registrationpage.Enterusername("user3451887");
    Registrationpage.EnterEmail("[email protected]");
}

Upvotes: 1

Related Questions