Selenium Testing

instalar

instalar google chrome

sudo apt-get install google-chrome-stable

proyecto

Mi proyecto

Instalar

Chrome Driver

https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/

Pasos:

Descargarlo

You can find the latest ChromeDriver on its official download page. Now execute below commands to configure ChromeDriver on your system.

sudo mv chromedriver /usr/bin/chromedriver

sudo chown root:root /usr/bin/chromedriver

sudo chmod +x /usr/bin/chromedriver

dependencias

  <!--
                Test Containers
        -->
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>${testcontainers.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>selenium</artifactId>
            <version>${testcontainers.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>         
            <version>${selenium-remote-driver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium-remote-driver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.141.59</version>
        </dependency>
        
           <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.29</version>
</dependency>
        
        
        
        
         <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

Codigo

package com.bpb.book;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testcontainers.containers.BrowserWebDriverContainer;

public class AppControllerTest {

    String url = "http://192.168.0.5:8080/krazosguide";
    @FindBy(id = "greeting")
    private WebElement greeting;

    @Rule
    public BrowserWebDriverContainer chrome
            = new BrowserWebDriverContainer()
                    .withCapabilities(new ChromeOptions());

    @Test
    public void viewTest() {
        RemoteWebDriver driver = chrome.getWebDriver();

        driver.get(url + "/mvc/app/view");

//        WebDriverWait wait = new WebDriverWait(driver, 10);// 1 minute 
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("country")));

        WebElement element = driver.findElement(By.name("country"));

        String country = element.getAttribute("value");
        assertEquals("Panama", country);
        driver.close();
    }

    @Test
    public void cdiTest() {
        RemoteWebDriver driver = chrome.getWebDriver();
        driver.get(url + "/mvc/app/cdi");
        WebElement element = driver.findElement(By.id("message"));
        String message = element.getAttribute("innerHTML");
        assertEquals("Welcome from CDI", message);
        

        driver.close();
    }

    @Test
    public void viewable() {
        RemoteWebDriver driver = chrome.getWebDriver();
        driver.get(url + "/mvc/app/viewable");
        WebElement element = driver.findElement(By.id("message"));
        String message = element.getAttribute("innerHTML");
        assertEquals("Welcome from viewable", message);
        driver.close();
    }

//    @Test
//    public void simplePlainSeleniumTest() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        driver.get("https://wikipedia.org");
//        WebElement searchInput = driver.findElementByName("search");
//
//        searchInput.sendKeys("Rick Astley");
//        searchInput.submit();
//
//        WebElement otherPage = driver.findElementByLinkText("Rickrolling");
//        otherPage.click();
//
//        boolean expectedTextFound = driver.findElementsByCssSelector("p")
//                .stream()
//                .anyMatch(element -> element.getText().contains("meme"));
//
//        assertTrue("The word 'meme' is found on a page about rickrolling", expectedTextFound);
//    }
//    @Test
//    public void helloJsp() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        driver.get("http://localhost:8080/krazosguide/mvc/app/view");
//        WebElement searchInput = driver.findElementByName("search");
//
//        searchInput.sendKeys("Avbravo");
//        searchInput.submit();
//
//        WebElement otherPage = driver.findElementByLinkText("Rickrolling");
//        otherPage.click();
//
//        boolean expectedTextFound = driver.findElementsByCssSelector("p")
//                .stream()
//                .anyMatch(element -> element.getText().contains("meme"));
//
//        assertTrue("The word 'meme' is found on a page about rickrolling", expectedTextFound);
//    }
//    @Test
//    public void book1() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//        driver.get("http://book.theautomatedtester.co.uk/chapter4");
//        WebElement element = driver.findElement(By.id("selectLoad"));
//        String value = element.getAttribute("value");
//        assertEquals("Click to load the select below", value);
//        driver.close();
//    }
//    @Test
//    public void google() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//        try{
//            driver.get("https://google.com/ncr");
//            driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
//            WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
//            System.out.println(firstResult.getAttribute("textContent"));
//        } finally {
//            driver.quit();
//        }
//    }
//    @Test
//    public void XPath() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        driver.get("http://http://192.168.0.5:8080/krazosguide/index.html");
////         WebElement element = driver.findElement(By.id("h12"));
////         WebElement element = driver.findElement(By.id("country"));
//        //    WebElement element = driver.findElement(By.id("search"));
//
////     wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("/html/body/input")));
////   WebElement element = driver.findElementByName("search");
//
//
//        WebDriverWait wait = new WebDriverWait(driver, 10);// 1 minute 
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("///html/body/input")));
////        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"name\"]")));
//        
//       //  WebElement element =driver.findElement(By.xpath("//input[@value='paypal']"));  
////         WebElement element =driver.findElement(By.name("input"));  
//         WebElement element =driver.findElement(By.xpath("///html/body/input"));  
////  driver.findElement(By.name("search")).sendKeys("selenium");
//        
//
//        String country = element.getAttribute("value");
//        assertEquals("Panama", country);
//        driver.close();
//    }
//    @Test
//    public void indexhtml() {
//        RemoteWebDriver driver = chrome.getWebDriver();
////driver.switchTo().frame("UWPIFrame");
//        //driver.get("http://localhost:8080/krazosguide/mvc/app/view");
//        driver.get("http://192.168.0.5:8080/krazosguide/index.html");
////         WebElement element = driver.findElement(By.id("h12"));
////         WebElement element = driver.findElement(By.id("country"));
//        //    WebElement element = driver.findElement(By.id("search"));
//
////     wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("/html/body/input")));
////   WebElement element = driver.findElementByName("search");
//
//
//        WebDriverWait wait = new WebDriverWait(driver, 10);// 1 minute 
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("search")));
////  driver.findElement(By.name("search")).sendKeys("selenium");
//        WebElement element = driver.findElement(By.name("search"));
//
//        String country = element.getAttribute("value");
//        assertEquals("Panama", country);
//        driver.close();
//    }
//    @Test
//    public void view() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        driver.get("http://192.168.0.5:8080/krazosguide/mvc/app/view");
//
//
//        WebDriverWait wait = new WebDriverWait(driver, 10);// 1 minute 
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("country")));
////  driver.findElement(By.name("search")).sendKeys("selenium");
//        WebElement element = driver.findElement(By.name("country"));
//
//        String country = element.getAttribute("value");
//        assertEquals("Panama", country);
//        driver.close();
//    }
//    @Test
//    public void google() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        WebDriverWait wait = new WebDriverWait(driver, 10);
//        try {
//            driver.get("https://google.com/ncr");
//            driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
//            WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
//            System.out.println(firstResult.getAttribute("textContent"));
//            assertEquals(firstResult.getAttribute("textContent"), "[Show more]");
//        } finally {
//            driver.quit();
//        }
//    }
//   @Test
//    public void book() {
//        RemoteWebDriver driver = chrome.getWebDriver();
//
//        driver.get("http://localhost:8080/krazosguide/mvc/app/view");
//        WebElement element = driver.findElement(By.id("login"));
//    String value = element.getAttribute("value");
//    assertEquals("Login", value);
//    }
//    @Test
//    public void viewxpaht() {
//        RemoteWebDriver driver = chrome.getWebDriver(); 
//
//        driver.get("http://192.168.0.5:8080/krazosguide/index.html");
//
//        
//// 
//       String heading = driver.findElement(By.xpath("/html/body/h2"))
//           .getText();
//       assertEquals("Welcome from view", heading);
//    }
}

Last updated

Was this helpful?