2

I'm trying to make a simple test to register in website - https://www.midomi.com/. I'm using c# and selenium but have stuck in selecting values from a dropdown list using Page object model. My project contains test class and page object folder with two classes inside her - home page and register page.

Register page: here I'm having problem to implement code to selecting values from the dropdown list

namespace MidomiRegisterPOM.PageObject
{
class RegisterPage
{
    private IWebDriver driver;

    //type your email
    [FindsBy(How = How.Id, Using = "email")]
    [CacheLookup]
    public IWebElement Email { get; set; }

    //type your username
    [FindsBy(How = How.Id, Using = "username")]
    [CacheLookup]
    public IWebElement UserName { get; set; }

    //type your password
    [FindsBy(How = How.Id, Using = "password")]
    [CacheLookup]
    public IWebElement Password { get; set; }

    //confirm your password
    [FindsBy(How = How.Id, Using = "confirm_password")]
    [CacheLookup]
    public IWebElement ConfirmPassword { get; set; }

    //here select from dropdown list your birth day, month and year       

    //mark privacy notice checkbox
    [FindsBy(How = How.Id, Using = "tos_pp")]
    [CacheLookup]
    public IWebElement PrivacyNotice { get; set; }

    //click Continue button
    [FindsBy(How = How.Id, Using = "submitLink")]
    [CacheLookup]
    public IWebElement ContinueButton { get; set; }

    public RegisterPage(IWebDriver driver)
    {
        this.driver = driver;
        PageFactory.InitElements(driver, this);
    }
    public void RegisterToSite()
    {
        Email.SendKeys("[email protected]");
        UserName.SendKeys("Tester");
        Password.SendKeys("testing");
        ConfirmPassword.SendKeys("testing");
        ContinueButton.Submit();
    }

I'm trying this but got an error: enter image description here

Is there any way to select it with [FindsBy] like I'm selecting email, username and password field? Thanks

Here is my code without using POM:

    var birthMonth = driver.FindElement(By.Id("birth_month"));
    var selectMonth = new SelectElement(birthMonth);
    selectMonth.SelectByValue("5");

3 Answers 3

0

Have you included all the necessary libraries?

using OpenQA.Selenium.Support.UI;

var birthMonth = driver.FindElement(By.Id("birth_month"));
var selectMonth = new SelectElement(birthMonth);
selectMonth.SelectByValue("5");

Or on one line:

new SelectElement(driver.FindElement(By.Id("birth_month"))).SelectByValue("5);
2
  • This working only on project with one class. Here i've test class, page object folder with two classes inside - home page and register page
    – larion
    Jun 22, 2018 at 13:15
  • Can you paste more of your code? Would copying the current methodology not work?
    – Jonnyboy
    Jun 22, 2018 at 13:26
0

You can use that function to select a element from e drop down.The element parameter would be your POM property and the option parameter is your element with value that you want to be clicked.

[FindsBy(How = How.Id, Using = "birth_month")]
[CacheLookup]
public IWebElement BirthMonth{ get; set; }

public void SelectElementFromDropDown(IWebElement element, string value)
{
    element.Click();

    var selecteElement = new SelectElement(element);

    selecteElement.SelectByValue(value);
}

Call it like this:

SelectElementFromDropDown(BirthMonth, 4);
2
  • In this code where to add Id("birth_month") and value "4"..
    – larion
    Jun 22, 2018 at 13:53
  • Thanks man. It's really works now. Because I have also BirthDay and BirthYear drop downs, I'm writing two more methods. It's not possible SelectElementFromDropDown method to takes 6 arguments. It's not cleanest code but good starting maybe
    – larion
    Jun 22, 2018 at 17:03
-1

How my final code looks. I wrote three methods for birth month, day and year. And called them by their names. I'm still trying to make method to takes 6 arguments if there's any way

namespace MidomiRegisterPOM.PageObject
{
class RegisterPage
{
    private IWebDriver driver;

    //type your email
    [FindsBy(How = How.Id, Using = "email")]
    [CacheLookup]
    public IWebElement Email { get; set; }

    //type your username
    [FindsBy(How = How.Id, Using = "username")]
    [CacheLookup]
    public IWebElement UserName { get; set; }

    //type your password
    [FindsBy(How = How.Id, Using = "password")]
    [CacheLookup]
    public IWebElement Password { get; set; }

    //confirm your password
    [FindsBy(How = How.Id, Using = "confirm_password")]
    [CacheLookup]
    public IWebElement ConfirmPassword { get; set; }

    // select from dropdown list your birth month
    [FindsBy(How = How.Id, Using = "birth_month")]
    [CacheLookup]
    public IWebElement BirthMonth { get; set; }

    // select from dropdown list your birth day
    [FindsBy(How = How.Id, Using = "birth_day")]
    [CacheLookup]
    public IWebElement BirthDay { get; set; }

    // select from dropdown list your birth year
    [FindsBy(How = How.Id, Using = "birth_year")]
    [CacheLookup]
    public IWebElement BirthYear { get; set; }

    //mark privacy notice checkbox
    [FindsBy(How = How.Id, Using = "tos_pp")]
    [CacheLookup]
    public IWebElement PrivacyNotice { get; set; }

    //click Continue button
    [FindsBy(How = How.Id, Using = "submitLink")]
    [CacheLookup]
    public IWebElement ContinueButton { get; set; }

    //method selecting birth month
    public void SelectMonth(IWebElement element, string option)
    {
        element.Click();

        var selecteElement = new SelectElement(element);

        selecteElement.SelectByText(option);

    }
    //method selecting birth day
    public void SelectDay(IWebElement element, string option)
    {
        element.Click();

        var selecteElement = new SelectElement(element);

        selecteElement.SelectByText(option);

    }
    //method selecting birth year
    public void SelectYear(IWebElement element, string option)
    {
        element.Click();

        var selecteElement = new SelectElement(element);

        selecteElement.SelectByText(option);

    }

    public RegisterPage(IWebDriver driver)
    {
        this.driver = driver;
        PageFactory.InitElements(driver, this);
    }
    public void RegisterToSite()
    {
        Email.SendKeys("[email protected]");
        UserName.SendKeys("Tester");
        Password.SendKeys("testing");
        ConfirmPassword.SendKeys("testing");
        SelectMonth(BirthMonth, "August");
        SelectDay(BirthDay, "15");
        SelectYear(BirthYear, "1999");
        PrivacyNotice.Click();
        ContinueButton.Click();
    }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.