Selenium测试自定义portlet

时间:2012-06-26 06:43:59

标签: selenium automated-tests liferay liferay-6

任何人都可以向我提供一个链接/文档,其中包含有关如何使用Selenium编写和测试自定义liferay portlet的信息。

我正在使用Liferay 6.1EE

由于

1 个答案:

答案 0 :(得分:0)

与其他测试相同,假设您想要以JUnit测试运行

package org.ood.selenium.test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import com.thoughtworks.selenium.SeleneseTestBase;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class MyTest extends SeleneseTestBase {
    WebDriver driver =null;
    @Before
    public void setUp() throws Exception {
        DesiredCapabilities dc = DesiredCapabilities.firefox(); 
        // dc.setCapability(FirefoxDriver.BINARY, new 
        //File("C:/Program Files (x86)/Mozilla Firefox/firefox.exe").getAbsolutePath()); 
        dc.setJavascriptEnabled(true); 
        // driver= new HtmlUnitDriver(true);//Not properly working
        driver = new FirefoxDriver(dc);
        String baseUrl = "http://localhost:8080/web/guest/home"; 
        selenium = new WebDriverBackedSelenium(driver, baseUrl);
    }

    /*  @Test
    public void homePage() throws Exception {
        final WebClient webClient = new WebClient();
            //HTMLUNit throws lots of errors
        webClient.setJavaScriptEnabled(true);
        webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

            public void timeoutError(HtmlPage htmlPage, long allowedTime,
                    long executionTime) {
                // TODO Auto-generated method stub

            }

            public void scriptException(HtmlPage htmlPage,
                    ScriptException scriptException) {
                System.out.println(scriptException.getMessage());

            }

            public void malformedScriptURL(HtmlPage htmlPage, String url,
                    MalformedURLException malformedURLException) {
                System.out.println(url);

            }

            public void loadScriptError(HtmlPage htmlPage, URL scriptUrl,
                    Exception exception) {
                // TODO Auto-generated method stub

            }
        });
        //webClient.setThrowExceptionOnFailingStatusCode(false);
        webClient.setThrowExceptionOnScriptError(false);
        final HtmlPage page = webClient.getPage("http://localhost:8080/web/guest/home");
        webClient.getCache().clear();

        page.getElementById("sign-in").click();
        page.getElementById("_58_login").type("test@liferay.com");
        page.getElementById("_58_password").type("test@liferay.com");
        final HtmlButton submit = (HtmlButton)page.getByXPath("//input").get(0);
        submit.click();


        //assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

        System.out.println("Title= " +page.getTitleText());
        final String pageAsXml = page.asXml();
        // assertTrue(pageAsXml.contains("<body class=\"composite\">"));
        System.out.println("pageAsXml=" +pageAsXml);

        final String pageAsText = page.asText();
        //assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));

        System.out.println("pageAsText="+pageAsText);
        webClient.closeAllWindows();
    }*/

    @Test //Using Graphical GUI
    public void testMytest() throws Exception {

        //driver.get("http://localhost:8080/web/guest/home");

        selenium.open("/");
        //selenium.click("id=sign-in");
        driver.findElement(By.id("sign-in")).click();
        driver.findElement(By.name("_58_login")).clear();
        driver.findElement(By.name("_58_login")).sendKeys("test@liferay.com");
        driver.findElement(By.name("_58_password")).sendKeys("123");
        //driver.findElement(By.name("_58_login")).sendKeys("test@nsn.com");
        //driver.findElement(By.name("_58_password")).sendKeys("test");
        driver.findElement(By.className("aui-button-input-submit")).submit();
        //selenium.setSpeed("1000");
        //selenium.waitForPageToLoad("5000");
        //Select a nested div( the NodeTree)
        driver.findElement(By.xpath("//div[contains(@class,'aui-tree-node-content')" +
                " and (contains(.,'TreeNodeX'))]//div[contains(@class,'aui-tree-hitarea')]"))
                .click();
        //selenium.captureScreenshot("d:/_del/sel_screen.png");//this throws an error
        //workaround
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("d:/_del/sel_screen.png"));

        System.out.println("Bodytex=[" +selenium.getBodyText() +"]End Body Text");

        String[] titles= selenium.getAllWindowTitles();
        System.out.println("Titles Start-");
        for (int i = 0; i < titles.length; i++) {
            System.out.println(titles[i]);
        }
        System.out.println("Titles End");
        verifyEquals(true, selenium.getBodyText().contains("Site View"));
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }


}