2012-06-26 38 views
5

Bất cứ ai cũng có thể cung cấp cho tôi một liên kết/tài liệu với thông tin về cách viết và kiểm tra các portlet liferay tùy chỉnh với Selenium.Kiểm tra selen cho các portlet tùy chỉnh

Tôi đang sử dụng Liferay 6.1EE

Cảm ơn

+2

http://www.liferay.com/community/wiki/-/wiki/Main/Cách + để + Chạy + Selenium + Kiểm tra –

+2

http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Selenium+Testing –

Trả lời

0

Tương tự như với các xét nghiệm khác, giả sử bạn muốn chạy như JUnit test

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("[email protected]"); 
     page.getElementById("_58_password").type("[email protected]"); 
     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("[email protected]"); 
     driver.findElement(By.name("_58_password")).sendKeys("123"); 
     //driver.findElement(By.name("_58_login")).sendKeys("[email protected]"); 
     //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(); 
    } 


} 
Các vấn đề liên quan