Example usage for org.openqa.selenium WebElement getAttribute

List of usage examples for org.openqa.selenium WebElement getAttribute

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getAttribute.

Prototype

String getAttribute(String name);

Source Link

Document

Get the value of the given attribute of the element.

Usage

From source file:BallerinaEditorUITest.java

License:Open Source License

@Test(dataProvider = "getData")
public void openBallerinaFile(String fileName) throws IOException, InterruptedException,
        ParserConfigurationException, SAXException, TransformerException, URISyntaxException {

    //creating relevant browser webdriver
    //TODO make this generic for multiple browsers
    WebDriver driver = new FirefoxDriver();
    //opening base page - welcome page this case
    driver.get(TestConstants.SERVER_URL);
    //once the open button available click it
    waitAndGetElementByXpath(driver, TestConstants.WELCOME_PAGE_OPEN_BUTTON_XPATH).click();
    //fill the location of the ballerina file to be opened
    URL BallerinaResourceLocation = BallerinaEditorUITest.class
            .getResource(TestConstants.BALLERINA_RESOURCE_FOLDER + File.separator + fileName + ".bal");
    waitAndGetElementByXpath(driver, TestConstants.FILE_OPEN_POPUP_LOCATION_INPUT_XPATH)
            .sendKeys(BallerinaResourceLocation.getPath());
    //wait for the open button in the pop-up window
    waitAndGetElementByXpath(driver, TestConstants.FILE_OPEN_POPUP_LOCATION_OPEN_XPATH).click();
    //wait for the SVG element where the diagram is rendered
    WebElement domElement = waitAndGetElementByXpath(driver, TestConstants.SVG_XPATH);
    //Getting inner HTML of the SVG node
    String dom = TestUtils.preprocessDOMContent(domElement.getAttribute("innerHTML"));
    //TODO Add mechanism to generate DOM files
    //TestUtils.fileWriter(dom, fileName + "DOM.xml");
    URL DOMResourceLocation = BallerinaEditorUITest.class
            .getResource(TestConstants.DOM_RESOURCE_FOLDER + File.separator + fileName + "DOM.xml");
    //destroying browser instance
    driver.quit();/*from ww w.  ja v  a 2  s . c o  m*/
    //checking inner content of the DOM element
    assertEquals("Rendered diagram of " + fileName + "is not equal to the expected diagram",
            TestUtils.fileReader(DOMResourceLocation.getPath()), dom);
}

From source file:ValidSubmission.java

public static void ThenJobsTitle_ShortDesc_FullDescIs() {
    // int j =0;/*w  w  w.  j av a2s. c  o  m*/
    // Grab the table 
    WebElement table = driver.findElement(By.id("jobListings"));

    //Get number of rows in table 
    int numOfRow = table.findElements(By.tagName("tr")).size();
    List<String> jobLinkUrl = new ArrayList(numOfRow);

    for (int j = 1; j <= (numOfRow - 2); j = (j + 2)) {

        String first_part_jobShortDesc_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobShortDesc_xpath = "]/td/span";

        String first_part_jobLinkTitle_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobLinkTitle_xpath = "]/td/a";

        String first_part_jobLinkUrl_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobLinkUrl_xpath = "]/td/a";

        WebElement webJobLinkUrl = driver
                .findElement(By.xpath(first_part_jobLinkUrl_xpath + (j + 2) + second_part_jobLinkUrl_xpath));
        jobLinkUrl.add(webJobLinkUrl.getAttribute("href"));
        //                   
        String final_jobLinkTitle_xpath = first_part_jobLinkTitle_xpath + (j + 2)
                + second_part_jobLinkTitle_xpath;
        String final_jobShortDesc_xpath = first_part_jobShortDesc_xpath + (j + 3)
                + second_part_jobShortDesc_xpath;

        WebElement webJobLinkTitle = driver.findElement(By.xpath(final_jobLinkTitle_xpath));
        String jobLinkTitle = webJobLinkTitle.getText();

        WebElement webJobShortDesc = driver.findElement(By.xpath(final_jobShortDesc_xpath));
        String jobShortDesc = webJobShortDesc.getAttribute("innerHTML");

        System.out.println("Job Title = " + jobLinkTitle);
        System.out.println();

        System.out.println("Short Description of Job = " + jobShortDesc);
        System.out.println();

        System.out.println("Link URL of Job" + jobLinkUrl);
        System.out.println();
    }

    //        // GoTo 'Full Job Description'
    for (int i = 0; i < jobLinkUrl.size(); i++) {

        driver.navigate().to(jobLinkUrl.get(i));
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //          
        WebElement webJobFullDescData = driver
                .findElement(By.xpath("//*[@id='jobDetails']/div[2]/table/tbody/tr[1]/td"));
        String jobFullDescData = webJobFullDescData.getText();
        //     
        System.out.println("Job Full Description Data" + jobFullDescData);
        System.out.println();

    }
}

From source file:DesktopChosenIT.java

License:Apache License

/**
 * Goal: verify that tab navigation is possible when Chosen is within a form.
 *///  ww w  .  ja va 2 s.  c o m
@Test
public void tabNavigation() {
    // Given
    loadTestCase(new TabNavigation());

    // When
    webDriverWait().until(elementToBeClickable(By.id("firstTextBox")));

    webDriver.switchTo().activeElement().sendKeys(Keys.TAB);
    webDriver.switchTo().activeElement().sendKeys(Keys.TAB);

    // at this point, focus is on the Chosen widget
    final String searchText = "ferr";
    webDriver.switchTo().activeElement().sendKeys(searchText);

    final WebElement inputBox = getInput();

    // Then
    webDriverWait().until(new Predicate<WebDriver>() {
        @Override
        public boolean apply(@Nullable WebDriver input) {
            String value = inputBox.getAttribute("value");
            return value != null && value.equals(searchText);
        }
    });
}

From source file:UnitTest1.java

@Test
public void comparisonFBloginButton() throws InterruptedException {
    String exePath = "C:\\Unit Testing\\Drivers\\chromedriver.exe";
    System.setProperty("webdriver.chrome.driver", exePath);
    WebDriver driver = new ChromeDriver();

    driver.get("https://www.facebook.com");

    String actualTitle = "u_0_o";

    WebElement test = driver.findElement(By.id("u_0_o"));
    assertEquals(actualTitle, test.getAttribute("id"));
    Thread.sleep(5);//from   w  w  w  . j  a v a 2s  .c o m
    driver.quit();

}

From source file:NumOfWHGamesTest.java

@Test
public void testCountNumOfGames() throws NoSuchElementException {

    WebElement game;
    String[] hrefArray = new String[500];
    int gameCount = 1;
    String hrefGame;/*from w  ww.j av a  2 s.co m*/
    String iStr;
    String gamesXPath1 = "html/body/div[2]/div/div/div[2]/div[3]/section/div/div/div/div[";
    String gamesXPath2 = "]/figure/a[1]";
    String gamesCountXPath = "html/body/div[2]/div/div/div[2]/div[3]/section/div/div/div";

    game = driver2.findElement(By.xpath(gamesXPath1 + String.valueOf(1) + gamesXPath2));
    hrefGame = game.getAttribute("href");
    hrefArray[0] = hrefGame;

    while (hrefGame != null) {
        try {
            gameCount++;
            iStr = String.valueOf(gameCount);
            game = driver2.findElement(By.xpath(gamesXPath1 + iStr + gamesXPath2));
            hrefGame = game.getAttribute("href");
            if (hrefGame != null)
                hrefArray[gameCount - 1] = hrefGame;
        } catch (Exception e) {
            hrefGame = null;
        }
    }
    gameCount--;
    // return gameCount;
    int actualNumGames = gameCount;
    int expectedNumGames = numOfGames;
    assertEquals(actualNumGames, expectedNumGames);
}

From source file:administrationedit.setSeasonTemplate.java

public static void main(String[] args) throws InterruptedException {

    for (int y = 0; y < 2; y++) {

        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, 3);
        driver.manage().window().maximize();

        Random r = new Random();

        Faker faker = new Faker();

        driver.get("https://goldfish.dev.hexis.hr/demo/en/administration/accommodation/seasonTemplateAdd");

        adminLogin login = new adminLogin(driver);
        login.loginToAdmin("demo", "demo");

        int currentYear = Calendar.getInstance().get(Calendar.YEAR) + y;
        String curYear = String.valueOf(currentYear);

        // Input name of template
        driver.findElement(By.id("name")).sendKeys("Season-" + curYear);

        // Choose year
        Select year = new Select(driver.findElement(By.id("year")));
        year.selectByVisibleText(curYear);

        // Now select seasons
        WebElement tableId = driver.findElement(By.xpath("//*[@class='ui-calendar hasDatepicker']"));
        if (year.getFirstSelectedOption().getText().equals(curYear)) {
            String id = tableId.getAttribute("id");

            for (int i = 1; i <= 12; i++) {
                Thread.sleep(1000);
                for (int j = 1; j <= 7; j++) {
                    if (!driver.findElements(By.xpath(
                            "//*[@id='" + id + "']/div/div[" + i + "]/table/tbody/tr[1]/td[" + j + "]/a"))
                            .isEmpty()) {
                        driver.findElement(By.xpath(
                                "//*[@id='" + id + "']/div/div[" + i + "]/table/tbody/tr[1]/td[" + j + "]/a"))
                                .click();
                        break;
                    }/*w  ww. ja  v  a 2s.  c om*/
                }
                i++;
                int weeks = driver
                        .findElements(By.xpath("//*[@id='" + id + "']/div/div[" + i + "]/table/tbody/tr"))
                        .size();

                int test = 0;
                for (int j = 7; j >= 1; j--) {
                    if (!driver.findElements(By.xpath("//*[@id='" + id + "']/div/div[" + i + "]/table/tbody/tr["
                            + weeks + "]/td[" + j + "]/a")).isEmpty()) {
                        test = 1;
                        break;
                    } else
                        test = 2;
                }
                switch (test) {
                case 1:
                    for (int j = 7; j >= 1; j--) {
                        if (!driver.findElements(By.xpath("//*[@id='" + id + "']/div/div[" + i
                                + "]/table/tbody/tr[" + weeks + "]/td[" + j + "]/a")).isEmpty()) {
                            driver.findElement(By.xpath("//*[@id='" + id + "']/div/div[" + i
                                    + "]/table/tbody/tr[" + weeks + "]/td[" + j + "]/a")).click();
                            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[3]/div/select")));
                            Select season1 = new Select(driver.findElement(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[3]/div/select")));
                            int season1int = r.nextInt(season1.getOptions().size() - 1) + 1;
                            season1.selectByIndex(season1int);
                            driver.findElement(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[4]/div/a[2]"))
                                    .click();
                            break;
                        }
                    }
                    break;
                case 2:
                    for (int j = 7; j >= 1; j--) {
                        if (!driver.findElements(By.xpath("//*[@id='" + id + "']/div/div[" + i
                                + "]/table/tbody/tr[" + (weeks - 1) + "]/td[" + j + "]/a")).isEmpty()) {
                            driver.findElement(By.xpath("//*[@id='" + id + "']/div/div[" + i
                                    + "]/table/tbody/tr[" + (weeks - 1) + "]/td[" + j + "]/a")).click();
                            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[3]/div/select")));
                            Select season2 = new Select(driver.findElement(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[3]/div/select")));
                            int season2int = r.nextInt(season2.getOptions().size() - 1) + 1;
                            season2.selectByIndex(season2int);
                            driver.findElement(By.xpath(
                                    "//*[@id='tab-basic']/div[3]/div[1]/div/div[2]/div/div/div/div/div/div[4]/div/a[2]"))
                                    .click();
                            break;
                        }
                    }
                }
            }
        }

        // SAVE
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("//*[@id='page-wrapper']/div[2]/form/div[2]/div/div/div/div[3]/div/input")));
        driver.findElement(By.xpath("//*[@id='page-wrapper']/div[2]/form/div[2]/div/div/div/div[3]/div/input"))
                .click();
        if (driver.getTitle().equals("Accommodation season templates administration")) {
            driver.close();
        }
    }
}

From source file:akori.Features.java

public static void main(String[] args) throws IOException, InterruptedException {

    URL = "http://www.mbauchile.cl";

    Document doc = Jsoup.connect(URL).timeout(0).get();

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();

    driver.get(URL);/* www  .j  ava  2 s . c  o  m*/

    Elements e1 = doc.body().getAllElements();
    Element e = doc.body();
    PrintWriter writer = new PrintWriter("features.txt", "UTF-8");
    int i = 1;
    //        String[][] matrix = new String[e1.size()][10];

    //        traverse(e, 1, 1, "", 1, writer, driver);

    ArrayList<String> tags = new ArrayList<String>();
    System.out.println("");
    //        for (Element temp : e1) {
    //            if (!temp.nodeName().equals("br")) {
    //                writer.println(i + "," + temp.hashCode() + "," + temp.nodeName() + "," + temp.id());
    //                //System.out.println(i+","+temp.hashCode()+","+temp.nodeName());
    //                ++i;
    //            }
    //        }
    i = 1;
    for (Element temp : e1) {
        if (tags.indexOf(temp.tagName()) == -1) {
            tags.add(temp.tagName());
            List<WebElement> query = driver.findElements(By.tagName(temp.tagName()));
            for (WebElement temp1 : query) {
                Point po = temp1.getLocation();
                Dimension d = temp1.getSize();
                if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0) {
                    continue;
                }
                if (temp1.getTagName().equals("img"))
                    writer.println(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width + ","
                            + d.height + "," + temp1.getAttribute("class") + "," + temp1.getAttribute("src"));
                else if (temp1.getTagName().equals("a"))
                    writer.println(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width + ","
                            + d.height + "," + temp1.getAttribute("class") + "," + temp1.getAttribute("href"));
                else
                    writer.println(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width + ","
                            + d.height + "," + temp1.getAttribute("class") + "," + temp1.getText());
                ++i;
            }
        }
    }
    driver.quit();
    writer.close();
}

From source file:akori.FeaturesSelenide.java

public static void main(String[] args) throws Exception {
    System.out.println("esto es FeaturesSelenide");
    PrintWriter writer = new PrintWriter("features.txt", "UTF-8");
    try {//from ww  w. j a  v  a  2  s .c o  m

        String URL = "http://www.mbauchile.cl";

        open(URL);

        SelenideElement s = $(By.tagName("body"));

        Document doc = Jsoup.connect(URL).timeout(0).get();
        Elements e1 = doc.body().getAllElements();

        ArrayList<String> tags = new ArrayList<String>();

        int i = 1;
        for (Element temp : e1) {
            if (tags.indexOf(temp.tagName()) == -1) {
                tags.add(temp.tagName());
                ElementsCollection query = $$(By.tagName(temp.tagName()));
                for (SelenideElement temp2 : query) {
                    WebElement temp1 = temp2.toWebElement();
                    Point po = temp1.getLocation();
                    Dimension d = temp1.getSize();
                    if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0) {
                        continue;
                    }
                    if (temp1.getTagName().equals("img")) {
                        writer.print(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width
                                + "," + d.height + "," + temp1.getAttribute("class") + ","
                                + temp1.getAttribute("src"));
                        int j = 1;
                        for (j = 1; !temp2.equals(s); ++j) {
                            temp2 = temp2.parent();
                            if (j > 100)
                                break;
                        }
                        writer.println("," + j);
                    } else if (temp1.getTagName().equals("a")) {
                        writer.println(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width
                                + "," + d.height + "," + temp1.getAttribute("class") + ","
                                + temp1.getAttribute("href"));
                        int j = 1;
                        for (j = 1; !temp2.equals(s); ++j) {
                            temp2 = temp2.parent();
                            if (j > 100)
                                break;
                        }
                        writer.println("," + j);
                    } else {
                        writer.println(i + "," + temp1.getTagName() + "," + po.x + "," + po.y + "," + d.width
                                + "," + d.height + "," + temp1.getAttribute("class") + "," + temp1.getText());
                        int j = 1;
                        for (j = 1; !temp2.equals(s); ++j) {
                            temp2 = temp2.parent();
                            if (j > 100)
                                break;
                        }
                        writer.println("," + j);
                    }
                    ++i;

                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    writer.close();
}

From source file:applicationdriverlayer.pageobjects.squash.booking.CourtAndTimeSlotChooserPage.java

License:Apache License

public Multimap<Integer, java.time.LocalTime> getBookedStartTimes() {

    // Iterate over cancellation buttons to find booked courts
    Multimap<Integer, java.time.LocalTime> bookedStartTimes = ArrayListMultimap.create();
    List<java.time.LocalTime> startTimes = getAllPossibleBookingStartTimes();
    for (WebElement element : cancellationButtons) {
        Integer court = Integer.parseInt(element.getAttribute("data-court"));
        java.time.LocalTime time = getStartTimeFromTimeSlot(
                Integer.parseInt(element.getAttribute("data-time_slot")), startTimes);

        bookedStartTimes.put(court, time);
    }//from   w  w  w . j a va  2 s .c  o  m
    return bookedStartTimes;
}

From source file:at.ac.tuwien.big.testsuite.impl.selenium.TableTest.java

License:Apache License

private Integer getDiceValue(WebDriver driver) {
    String src = null;//from ww w . j a  va  2s .  co  m

    if (exists(driver, By.id(diceFormPrefix + "diceImage"))) {
        src = driver.findElement(By.id(diceFormPrefix + "diceImage")).getAttribute("src");
    } else if (exists(driver, By.id(diceFormPrefix + "dice"))) {
        WebElement diceElement = driver.findElement(By.id(diceFormPrefix + "dice"));
        src = diceElement.getAttribute("src");

        if (src == null) {
            List<WebElement> imgElems = diceElement.findElements(By.xpath(".//img"));

            if (!imgElems.isEmpty()) {
                diceElement = imgElems.get(0);
                src = diceElement.getAttribute("src");
            }
        }
    }

    if (src == null) {
        assertFalse(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Could not retrieve dice value", true);
    }

    Matcher matcher = DICE_VALUE_PATTERN.matcher(src);

    if (!matcher.matches()) {
        assertFalse(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Could not retrieve dice value", true);
    }

    return Integer.parseInt(matcher.group(1));
}