2017-06-15 67 views
0

有人可以幫助我找到一個鏈接字段 測試網站:http://demosite.center/wordpress/wp-login.php 用戶名:admin密碼 :demo123硒場定位

測試用例:點擊左邊有側板的帖子,然後單擊添加新的。

我無法找到登錄後的'帖子'鏈接。腳本是引發此錯誤java.lang.reflect.InvocationTargetException

public class NewPost { 

WebDriver driver; 

    public NewPost(WebDriver localdriver){ 
     this.driver = localdriver; 
    } 

    WebElement box=driver.findElement(By.xpath("//*[@id='menu-posts']/a")); 
    List<WebElement> links = box.findElements(By.tagName("a")); 
    WebElement posts = links.get(1); 


    @FindBy(how=How.XPATH, using="//*[@id='wpbody-content']/div[4]/h2/a") 
    WebElement addNew; 

    @FindBy(how=How.XPATH, using="//*[@id='title']") 
    WebElement postTitle; 

    @FindBy(how=How.XPATH, using="//*[@id='content']") 
    WebElement content; 

    @FindBy(how=How.XPATH, using="//*[@id='publish']") 
    WebElement publishButton; 

    @FindBy(how=How.ID, using="message") 
    WebElement postPublishedMsg; 

    public void addNewPost(String titleText, String contentText){ 

     posts.click(); 
     addNew.click(); 
     postTitle.sendKeys(titleText); 
     content.sendKeys(contentText); 
     publishButton.click(); 

     if(postPublishedMsg.getText().contains("Post published")){ 
      System.out.println("Post published successfully"); 
     } 


    } 


} 

測試類:

@Test 
public class VerifyNewPost { 

    public void checkNewPost(){ 

     WebDriver driver = BrowserFactory.startBrowser("firefox", "http://demosite.center/wordpress/wp-login.php"); 

     LoginPageNew login_page = PageFactory.initElements(driver, LoginPageNew.class); 

     login_page.loginWordpress("admin", "demo123"); 

     NewPost post = PageFactory.initElements(driver, NewPost.class); 

     post.addNewPost("This is new post title", "This is new Post content"); 


    } 
} 
+0

不是一個真正的答案,但我會推薦Codeception框架。然後它會像這樣簡單:$ I-> click('Posts'); – sunomad

回答

0

爲了點擊添加新按鈕,您必須首先懸停在鏈接後鼠標。

您可以使用Actions類來實現此目的。

請試試這個:

WebElement postlink = driver.findElement(By.xpath("//a[contains(text(),'Posts')]")); 
Actions action = new Actions(driver); 
action.moveToElement(postlink); 
// Click on 'Add New' button 
driver.findElement(By.xpath("//li[@id='menu-posts']//a[contains(text(),'Add New')]")).click(); 

我沒有用頁廠。對不起。

0

這裏是回答你的問題:

  1. 一旦你在Dashboard頁面點擊Posts環節中,元素按照PageFactory是:

    @FindBy(how=How.XPATH,using="//li[@id='menu-posts']/a") 
    WebElement posts; 
    
  2. 構建方法點擊Posts鏈接:

    public void click_Posts() 
    { 
        posts.click(); 
        System.out.println("Clicked on Posts on Dashboard page"); 
    } 
    
  3. 現在從VerifyNewPost類呼叫click_Posts()方法來執行點擊Posts鏈接。

  4. 在登錄到您要登錄的網站Dashboard頁面後,需要定義頁面對象和相關方法。

  5. 在當你點擊鏈接PostsDashboard,你會被重定向到Posts頁面,您需要定義以點擊Add New鏈接的頁面對象和相關方法。

讓我知道這個答案是否是您的問題。