Example usage for org.openqa.selenium By id

List of usage examples for org.openqa.selenium By id

Introduction

In this page you can find the example usage for org.openqa.selenium By id.

Prototype

public static By id(String id) 

Source Link

Usage

From source file:scormADL2004.java

License:Open Source License

@Before
public void setUp() throws IOException, InterruptedException {
    driver = new InternetExplorerDriver();
    wait = new WebDriverWait(driver, 60);
    String testSuiteURL = System.getProperty("testSuiteURL");
    String moodleURL = System.getProperty("moodleURL");
    if (testSuiteURL == null) {
        throw new IOException("ADL SCORM 2004 Test Suite URL not specified");
    }//w ww .  ja v  a  2 s.  c  om
    if (moodleURL == null) {
        throw new IOException("Moodle Test Site URL not specified");
    }
    if (username1 == null) {
        username1 = "joestudent";
        password1 = "joestudent";
    }
    if (username2 == null) {
        username2 = "marylearner";
        password2 = "marylearner";
    }
    driver.get(testSuiteURL);
    // Overcome Selenium IE Page load Bug.
    driver.switchTo().frame("instructions");
    wait.until(presenceOfElementLocated(By.id("New")));
    ((JavascriptExecutor) driver).executeScript("newTest();");
    wait.until(presenceOfElementLocated(By.id("pname")));
    driver.findElement(By.id("pname")).sendKeys("Moodle");
    driver.findElement(By.id("pversion")).sendKeys("2011033003");
    driver.findElement(By.id("pvname")).sendKeys("MoodleHQ");
    driver.switchTo().window("");
    driver.switchTo().frame("controls");
    driver.findElement(By.id("continue")).click();
    driver.switchTo().window("");
    driver.switchTo().frame("instructions");
    wait.until(presenceOfElementLocated(By.id("learner1Name")));
    driver.findElement(By.id("learner1Name")).sendKeys("Moodle Learner 1");
    driver.findElement(By.id("learner1ID")).sendKeys(username1);
    driver.findElement(By.id("learner2Name")).sendKeys("Moodle Learner 2");
    driver.findElement(By.id("learner2ID")).sendKeys(username2);
    driver.switchTo().window("");
    driver.switchTo().frame("controls");
    driver.findElement(By.id("continue")).click();
    driver.switchTo().window("");
    ((JavascriptExecutor) driver).executeScript("window.open('" + moodleURL + "', 'moodleWindow');");
    driver.switchTo().window("moodleWindow");
}

From source file:scormADL2004.java

License:Open Source License

@Test
/**/*from www.ja  v  a 2  s  .c o  m*/
 * navigate inside ADL SCORM 2004 Test Course to check for packages uploaded.
 */
public void loginCourse() throws IOException {
    driver.findElement(By.linkText("SCORM 2004 ADL test course")).sendKeys(Keys.ENTER);
    wait.until(presenceOfElementLocated(By.id("username")));
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("password")).clear();
    //Login username password as specified
    driver.findElement(By.id("username")).sendKeys(username1);
    driver.findElement(By.id("password")).sendKeys(password1);
    loginSession = 1;
    driver.findElement(By.id("loginbtn")).click();
    fetchAllContent();
}

From source file:scormADL2004.java

License:Open Source License

public void switchLoginSession(int toLogin) {
    // To be safe and avoid Selenium Native click element, using submit wherever possible, to be fixed in 2.5.0
    WebElement element;/*  w w  w .  j  av  a 2 s .com*/
    driver.switchTo().window("moodleWindow");
    // Check if already logged in
    if (!isElementPresent(By.linkText("Login"))) {
        //driver.findElement(By.linkText("Logout")).click();
        // Workaround for Selenium bug - 1020, to be fixed in 2.5.0
        driver.findElement(By.linkText("Logout")).sendKeys(Keys.ENTER);
        wait.until(presenceOfElementLocated(By.linkText("SCORM 2004 ADL test course")));
        driver.findElement(By.linkText("SCORM 2004 ADL test course")).click();
    }
    if (toLogin != 2) {
        wait.until(presenceOfElementLocated(By.id("username")));
        driver.findElement(By.id("username")).clear();
        element = driver.findElement(By.id("password"));
        element.clear();
        driver.findElement(By.id("username")).sendKeys(username1);
        element.sendKeys(password1);
        loginSession = 1;
    } else {
        wait.until(presenceOfElementLocated(By.id("username")));
        driver.findElement(By.id("username")).clear();
        element = driver.findElement(By.id("password"));
        driver.findElement(By.id("username")).sendKeys(username2);
        element.sendKeys(password2);
        loginSession = 2;
    }
    element.submit();
}

From source file:scormADL2004.java

License:Open Source License

public String readUserInstructions() {
    // Switch to window of TestSuite
    driver.switchTo().window("");
    driver.switchTo().frame("currentInstructions");
    return driver.findElement(By.id("userInstruction")).getText();
}

From source file:scormADL2004.java

License:Open Source License

public void answerDefaultUI(int scoNo) {
    driver.switchTo().window("moodleWindow");
    driver.switchTo().frame("scorm_object");
    System.out.println("Testing Activity " + scoNo + ". Please Wait... ");
    driver.findElement(By.id("tRadio0")).click();
    if (scoNo == 1) {
        driver.findElement(By.id("fRadio1")).click();
    } else {//from   w w w. jav a  2s. co  m
        driver.findElement(By.id("tRadio1")).click();
    }
    driver.findElement(By.id("submit")).click();
    System.out.println("Testing of Activity Completed");
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Execute CM-01 Test//from ww  w .  j  a v a  2  s  .  c  o  m
 */
public void handleCM01Test() {
    String[] scoUrl;
    scoUrl = new String[2];
    String currentUrl = driver.getCurrentUrl();
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    driver.switchTo().frame("scorm_object");
    wait.until(presenceOfElementLocated(By.id("tRadio0")));
    //WebElement elementIR = driver.findElement(By.id("fRadio0"));
    //elementIR.click();
    wait.until(presenceOfElementLocated(By.xpath("//label[contains(.,'Did the LMS')]")));
    driver.findElement(By.id("tRadio0")).click();
    driver.findElement(By.id("fRadio1")).click();
    driver.findElement(By.id("fRadio2")).click();
    driver.findElement(By.id("submit")).click();
    //elementIR.submit();
    /*driver.switchTo().window("moodleWindow");
    //driver.findElement(By.id("ygtvlabelel2")).click();
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    System.out.println("done 1st");
    driver.switchTo().frame("scorm_object");
    //wait.until(presenceOfElementLocated(By.id("submit")));
    System.out.println("done 2nd");*/
    wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
    System.out.println("Testing Activity 2. Please Wait...");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    driver.switchTo().window("moodleWindow");
    scoUrl[0] = driver.findElement(By.xpath("//span[contains(.,'Activity 2')]")).getAttribute("title");
    driver.get(currentUrl + '?' + scoUrl[0]);
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    driver.switchTo().frame("scorm_object");
    wait.until(presenceOfElementLocated(By.xpath("//label[contains(.,'Did the LMS')]")));
    driver.findElement(By.id("fRadio0")).click();
    driver.findElement(By.id("tRadio1")).click();
    driver.findElement(By.id("submit")).click();
    driver.switchTo().window("moodleWindow");
    System.out.println("Testing Activity 3. Please Wait...");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    scoUrl[1] = driver.findElement(By.xpath("//span[contains(.,'Activity 3')]")).getAttribute("title");
    driver.get(currentUrl + '?' + scoUrl[1]);
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Handle / Launch SCO's/*from   ww  w .ja va  2s .c  o  m*/
 */
public void defaultSCOHandler() {
    // Handle Selenium IE Bug by explicit wait
    int scoCount = fetchAllSCO();
    int scoNo = 1;
    String userInstruction, activityName = null;
    // Not infinite loop, testing based on TestSuite Instrcutions
    while (scoCount > 0) {
        //First Activity does not need to be launched, launch after first
        driver.switchTo().window("moodleWindow");
        driver.switchTo().frame("scorm_object");
        wait.until(presenceOfElementLocated(By.id("teststatus")));
        //wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'progress...')]")));
        // Allow loading time, specially for UI tests
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        userInstruction = readUserInstructions();
        if (userInstruction.contains("LMS User Interface")) {
            answerDefaultUI(scoNo);
            driver.switchTo().window("moodleWindow");
        } else {
            // Print only for 1st Activity.
            if (scoNo == 1) {
                System.out.println("Testing Activity. Please Wait... ");
            }
            driver.switchTo().window("moodleWindow");
            driver.switchTo().frame("scorm_object");
            //Check only for "Completed." as some packages contain extra spaces
            wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
            System.out.println("Testing of Activity Completed.");
            driver.switchTo().window("moodleWindow");
        }
        // Read instructions to launch new activity
        // First Activity does not need to be launched.

        userInstruction = readUserInstructions();
        if (!userInstruction.contains("relaunch")
                && !(userInstruction.contains("please exit the package/course"))
                && userInstruction.contains("launch")) {
            activityName = userInstruction.substring((userInstruction.lastIndexOf("nch ") + 4),
                    userInstruction.lastIndexOf("."));
            activityName = activityName.trim();
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.xpath("//span[contains(.,'" + activityName + "')]")).click();
            System.out.println("Testing Activity " + activityName + ". Please Wait... ");
            // Allow loading time of new Activity
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else if (userInstruction.contains("trigger a Continue Navigation Event")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.id("nav_next-button")).sendKeys(Keys.ENTER);
        } else if (userInstruction.contains("trigger a Previous Navigation Event")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.id("nav_prev-button")).sendKeys(Keys.ENTER);
        } else if (userInstruction.contains("relaunch")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.linkText("ADL 2004")).sendKeys(Keys.ENTER);
            launchTestPackage(userInstruction.substring((userInstruction.lastIndexOf("age ") + 4),
                    (userInstruction.lastIndexOf("to") - 1)));
        } else if (userInstruction.contains("please exit the package/course")) {
            if (userInstruction.contains("launch")) {
                driver.switchTo().window("moodleWindow");
                driver.findElement(By.linkText("ADL 2004")).sendKeys(Keys.ENTER);
                launchTestPackage(userInstruction.substring((userInstruction.lastIndexOf("age ") + 4),
                        userInstruction.lastIndexOf(".")));
            } else {
                System.out.println("Testing of Package Completed.");
                break;
            }
        } else if (userInstruction.isEmpty()) {
            // Launch last activity again.
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.xpath("//span[contains(.,'" + activityName + "')]")).click();
        }
        scoNo++;
    }
    // Old way of handling tests.
    /*for (int i = 1; i <= scoCount; i++) {
       //First Activity does not need to be launched.
       if(i != 1) {
    driver.findElement(By.id("ygtvlabelel"+i)).click();
       }
       driver.switchTo().frame("scorm_object");
       wait.until(presenceOfElementLocated(By.id("teststatus")));
       wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'progress...')]")));
       boolean uiTestPresent = isInstructionForUIPresent();
       if (uiTestPresent) {
    answerDefaultUI(i);
       } else {
    System.out.println("Testing Activity "+i+". Please Wait... ");
    //Check only for "Completed." as some packages contain extra spaces
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Completed.')]")));
    System.out.println("Testing of Activity "+i+" Completed.");
    driver.switchTo().window("moodleWindow");
       }
    }
    // Some test packages need to launch 
    System.out.println("Testing Completed.");
    driver.switchTo().window("");
    driver.switchTo().frame("controls");
    wait.until(presenceOfElementLocated(By.id("continue")));
    driver.findElement(By.id("continue")).click(); */
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Select All valid tests to perform in ADL 2004 Test Suite
 * @param testsToExecute/*from   ww  w .j a  v  a2 s  . c  o  m*/
 */
public void initialiseTestSuite(String[] testsToExecute) {
    driver.switchTo().window("");
    driver.switchTo().frame("instructions");
    Select multipleTestsSelect = new Select(driver.findElement(By.id("possible")));
    multipleTestsSelect.deselectAll();
    for (int i = 0; i < testsToExecute.length; i++) {
        multipleTestsSelect.selectByVisibleText(testsToExecute[i]);
    }
    // Workaround for Selenium bug - 1020, to be fixed in 2.5.0
    driver.findElement(By.id("add")).sendKeys(Keys.ENTER);
    driver.switchTo().window("");
    driver.switchTo().frame("controls");
    driver.findElement(By.id("continue")).click();
    try {
        driver.switchTo().alert().accept();
    } catch (NoAlertPresentException ex) {
    }

}

From source file:scormADL2004.java

License:Open Source License

public void launchTestPackage(String testPackageName) {
    // Check to see which user should launch the package.
    checkLoginSession();/*from ww  w  .j a va  2  s.  c  o  m*/
    driver.switchTo().window("moodleWindow");
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Recent activity')]")));
    if (testPackageName.equalsIgnoreCase("DDM")) {
        driver.findElement(By.partialLinkText(testPackageName + "a SCORM package")).sendKeys(Keys.ENTER);
    } else {
        driver.findElement(By.partialLinkText(testPackageName + " SCORM package")).sendKeys(Keys.ENTER);
    }
    // Workaround for WebDriver bug, Seleniu, IE.
    wait.until(presenceOfElementLocated(By.id("page-footer")));
    // Refresh Driver to crawl page again.
    driver.switchTo().window("moodleWindow");
    if (isElementPresent(By.id("n"))) {
        WebElement element = driver.findElement(By.id("n"));
        element.click();
        element.submit();
        //driver.findElement(By.xpath("//input[@value='Enter']")).click();
    }
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Handle / Launches test packages in order they are specified.
 * @param String[] testsToExecute/*from ww  w  .  j a  v  a  2  s.  co  m*/
 */
public void testsHandler(String[] testsToExecute) {
    if (testsToExecute[0] == "") {
        // No tests uploaded
        throw new NullPointerException("No specified tests found or uploaded.");
    }
    initialiseTestSuite(testsToExecute);
    driver.switchTo().window("");
    driver.switchTo().frame("currentInstructions");
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Login to the LMS')]")));
    // Check if current user login is the required user logged in
    checkLoginSession();
    driver.switchTo().window("moodleWindow");
    // Wait till Page completely loads - Selenium Bug
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Recent activity)]")));
    for (int i = 0; i < testsToExecute.length; i++) {
        System.out.println("Testing ADL SCORM 2004 " + testsToExecute[i] + " package...");
        launchTestPackage(testsToExecute[i]);
        //switch cannot compare string
        // TODO add handle for CM-01 Test
        if (testsToExecute[i].equalsIgnoreCase("CM-01")) {
            handleCM01Test();
        }
        // default case
        else {
            defaultSCOHandler();
        }
        driver.switchTo().window("moodleWindow");
        driver.findElement(By.linkText("Exit activity")).click();
        wait.until(presenceOfElementLocated(By.partialLinkText(testsToExecute[i] + " SCORM package")));
        driver.switchTo().window("");
        driver.switchTo().frame("controls");
        wait.until(presenceOfElementLocated(By.id("continue")));
        driver.findElement(By.id("continue")).sendKeys(Keys.ENTER);
    }
    driver.findElement(By.linkText("Logout")).click();
    System.out.println("Tested All Packages. The Script will now exit.");
}