Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Quality Assurance

Are there any code examples for this course (Introduction to Selenium)?

Just wondering if I have missed something. I'm only see the RSVP code in the Workspace, and Videos in the Downloads section. I'm having some issues with the Page Object section, and code samples would be helpful. Thank you.

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Neil,

It looks like proejct files for each video are not provided like in some courses.

Hopefully this will help you get back on track (I assume you fixed the error yesterday with your promise file)

in project root.

index.js
const fs = require("fs");

const selenium = require("selenium-webdriver");
const By = selenium.By;

//import file for HomePage file/module
const HomePage = require('./pages/home');

const driver = new selenium.Builder()
    .forBrowser("chrome")
    .build();

//new instance of HomePage Modile with selenium instance passed in
const homePage = new HomePage(driver);

homePage.open();

//list of invitee names
const invitees = [
    'Gonzalo Torres del Fierro',
    'Shadd Anderson',
    'George Aparece',
    'Shadab Khan',
    'Joseph Michael Casey',
    'Jennifer Nordell',
    'Faisal Albinali',
    'Taron Foxworth',
    'David Riesz',
    'Maicej Torbus',
    'Martin Luckett',
    'Joel Bardsley',
    'Reuben Varzea',
    'Ken Alger',
    'Amrit Pandey',
    'Rafal Rudzinski',
    'Brian Lynch',
    'Lupe Camacho',
    'Luke Fiji',
    'Sean Christensen',
    'Philip Graf',
    'Mike Norman',
    'Michael Hulet',
    'Brent Suggs'
 ];

//store By locators 

invitees.forEach(homePage.addInvitee, homePage);

homePage
    .findInviteeByName("David Riesz")
    .remove();

homePage
    .findInviteeByName("Jennifer Nordell")
    .toggleConfirmation();

driver.takeScreenshot().then((image, err) => {
    fs.writeFile("weird-layout.png", image, "base64",
        err => console.error(err));

});

homePage.toggleNonRespondersVisibility();

in "pages" directory

home.js
const By = require("selenium-webdriver").By;

class HomePage {

    constructor(driver) {
        this.driver = driver;
        this.locators = {
            inviteeForm: By.id("registrar"),
            //inviteeNameField: By.name("name"),
            inviteeNameField: By.css("#registrar input[name='name']"),
            toggleNonRespondersVisibility: By.css(".main > div input"),
            removeButtonForInvitee: invitee => By.xpath(`//span[text() = "${invitee}"]/../button[last()]`),
            inviteeByName: name => By.xpath(`//span[text() = "${name}"]/..`)
        };
    }

    open() {

        this.driver.get(process.env.URL);
    }

    addInvitee(name) {
        this.driver.findElement(this.locators.inviteeNameField)
            .sendKeys(name);

        this.driver.findElement(this.locators.inviteeForm).submit();
    }

    toggleNonRespondersVisibility() {

        this.driver.findElement(this.locators.toggleNonRespondersVisibility)
            .click();
    }

    findInviteeByName(name) {
        const el = this.driver
            .findElement(this.locators.inviteeByName(name));

        return new Invitee(el);
    }


}

class Invitee {
    constructor(element) {
        this.element = element;
        this.locators = {
            removeButton: By.css("button:last-child"),
            confirmedCheckbox: By.css("input[type='checkbox']")

        };

    }

    remove() {
        this.element
            .findElement(this.locators.removeButton)
            .click();
    }

    toggleConfirmation() {
        this.element
            .findElement(this.locators.confirmedCheckbox)
            .click();

    }
}

module.exports = HomePage;

Thanks Jonathan Grieve, that worked. :)

I've still been getting Promise errors, but at least now the Chrome Browser doesn't crash so I can still work through the course. Thanks again.