vendredi 26 mai 2017

Automating github login through Selenium Webdriver

Hi guys I am trying to automate the github login process through selenium web driver in java , everything is fine and okay except for the very first time when the user does not exist github asks the user to allow the access as shown in the picture

enter image description here

I am trying to find out that if the title of the window is authorize application than run a specific flow and if not run the usual flow but its not finding out the title and the test fails on the authorization screen and if I try to find the authorize green button it says the element is not enabled

The code is below for help

package com.silexica.selenium.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class GitHubLoginTest {

    @Test
    public void githubLogin() {

        // Set our gecko driver
        System.setProperty("webdriver.gecko.driver", "geckodriver");

        // setting up the firefox driver
        WebDriver driver = new FirefoxDriver();

        // Open website
        driver.get("");

        // wait for the google icon button to load
        WebDriverWait githubbuttonwait = new WebDriverWait(driver, 10);
        WebElement githubloginbutton = githubbuttonwait.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("/html/body/div/div/div/div/div/form/ul/li/a[2]/img[2]")));

        // click the button if present
        githubloginbutton.click();

        // Find the email form field
        WebDriverWait githubemailfieldwait = new WebDriverWait(driver, 10);
        WebElement githubemailfield = githubemailfieldwait.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[1]/div/form/div[3]/input[1]")));

        // Type the random email to the form
        githubemailfield.sendKeys("");

        // Find the password form field
        WebDriverWait githubpasswordfieldwait = new WebDriverWait(driver, 10);
        WebElement githubpasswordfield = githubpasswordfieldwait.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[1]/div/form/div[3]/input[2]")));

        // Type password to the form. This needs not be unique.
        githubpasswordfield.sendKeys("");

        // find the sign up button
        WebDriverWait githubpasswordbutton = new WebDriverWait(driver, 10);
        WebElement passwordfieldsignupbutton = githubpasswordbutton.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[1]/div/form/div[3]/input[3]")));

        // click the sign up button
        passwordfieldsignupbutton.click();

        // verify that the authorization window has the given text
        if ((new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driverObject) {
                return driverObject.getTitle().equals("Authorize application");
            }
        })) {

            // find the allow access button button in google password window
            WebDriverWait githubpasswordbutton2 = new WebDriverWait(driver, 10);
            WebElement passwordfieldsignupbutton2 = githubpasswordbutton2
                    .until(ExpectedConditions.visibilityOfElementLocated(
                            By.xpath("/html/body/div[4]/div[1]/div[2]/div[1]/div[2]/div[1]/form/button")));

            // click the button
            passwordfieldsignupbutton2.click();

            // verify that we have logged into the dashboard
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driverObject) {
                    return driverObject.getTitle().equals("Silexica | Dashboard");
                }
            });

            driver.quit();

        } else {

            // Verify that whether we have logged into the dashboard and
            // continue the normal flow
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driverObject) {
                    return driverObject.getTitle().equals("Silexica | Dashboard");
                }
            });

            // close the browser
            driver.quit();

        }

    }

}

Any suggestions or improvements

Aucun commentaire:

Enregistrer un commentaire