2013-06-04 21 views
2

我是一个新的测试人员,我有两个关于测试脚本的问题,我从selenium ide导出测试脚本(导出到Junit4 webdriver),然后将其粘贴到我的eclipse indigo中。但是,当我运行我的测试脚本,它只会持续到
driver.findElement(By.name("j_idt61")).click();我的编码driver.findElement(By.linkText(“Logout”))。click();在测试脚本中没有执行

后的代码行实际上救一个线,是点击注销链接driver.findElement(By.linkText("Logout")).click();

但硒似乎无法找到LINKTEXT “Logout” ..

这是我的代码

package admin ; 

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class login { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
    driver = new FirefoxDriver(); 
    baseUrl = " here I put my website URL"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testLogin() throws Exception { 
    driver.get(baseUrl + "/admin/pages/login.xhtml"); 

    driver.findElement(By.id("j_username")).click(); 
    driver.findElement(By.id("j_username")).clear(); 
    driver.findElement(By.id("j_username")).sendKeys("sasa"); 
    driver.findElement(By.id("j_password")).click(); 
    driver.findElement(By.id("j_password")).clear(); 
    driver.findElement(By.id("j_password")).sendKeys("sasasamsudin"); 
    driver.findElement(By.name("j_idt61")).click(); 
    driver.findElement(By.linkText("Logout")).click(); 
    } 

    @After 
    public void tearDown() throws Exception { 
    driver.quit(); 
    String verificationErrorString = verificationErrors.toString(); 
    if (!"".equals(verificationErrorString)) { 
     fail(verificationErrorString); 
    } 
    } 

public boolean isElementPresent(By id) { 
    try { 
     driver.findElements(By.id("J_idt16:J_idt30")); 
     return true; 
    } catch (org.openqa.selenium.NoSuchElementException e) { 
     return false; 
    } 
    } 

public boolean isElementVisible(By id) 
{ 

    return driver.findElement(By.id("J_idt16:J_idt30")).isDisplayed(); 

} 

    private String closeAlertAndGetItsText() { 
    try { 
     Alert alert = driver.switchTo().alert(); 
     if (acceptNextAlert) { 
     alert.accept(); 
     } else {`enter code here` 
     alert.dismiss(); 
     } 
     return alert.getText(); 
    } finally { 
     acceptNextAlert = true; 
    } 
    } 
} 

请人帮我。我刚开始我的工作,确实需要这样AU tomation testing to be working properly .. THANKS :)

+3

请把你对正在运行的HTML或尝试与公共重现这个面对网站。没有它,我们什么都不能做。 – Arran

+0

@Arran,把IDE的HTML?你是这个意思吗 ? –

回答

3

请尝试使用driver.findElement(By.partialLinkText("Logout"));来代替,因为可能存在其他/空白字符,您没有看到。 partialLinkText()只检查链接是否包含您的文字,但linkText()会检查链接的整个文字是否与您的文字相同。

此外,请确保包含文本“注销”的元素是<a></a> - 因为linkText()partialLinkText()不会检查任何其他类型的元素。

编辑:

如果问题是该元素存在,但不可见,那么你就需要等待它变得可见:

// find the element 
WebElement anchor = driver.findElement(By.partialLinkText("Logout")); 

// create a wait with 10-second timeout 
WebDriverWait wait = new WebDriverWait(driver, 10); 

// wait for the element to become visible 
anchor = wait.until(ExpectedConditions.visibilityOf(anchor)); 

// proceed with test... 

或者,如果元素ISN “T将最终成为自身的可见的,你可以迫使它是可见的:

// find the element 
WebElement anchor = driver.findElement(By.partialLinkText("Logout")); 

// execute JavaScript to change the element's visibility 
((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.display = 'block';", anchor); 

// proceed with test... 
+0

海@Pat米克 我改变了我的编码,以paritalLinkText但仍然有以下错误: org.openqa.selenium.ElementNotVisibleException:元素是不可见的,所以可能无法与 进行交互我查了“”注销”是 .. Logout

+0

好的,我更新了我的答案 –

+0

thans @pat meeker –

0

我只是碰到了这个问题我自己。我导出了一组在Selenium IDE中正常工作的测试,但在导出后似乎不再起作用。你将不得不原谅我的代码示例,因为我使用Python而不是Java。所以在我的情况下,我不得不

driver.find_element_by_link_text("Logout").click() 

,我改变了这个

driver.find_element_by_xpath("//a[contains(text(),'Logout')]").click() 

这同样也适用于当你使用“By.LINK_TEXT”这只是交换它“By.XPATH”,然后用基于xpath的参数更新参数。

希望有所帮助。

先生-F

0

您可能有放入系统的问题之一:

1--谁的LINKTEXT =“注销”尚未启用,因此,你应该等到组件将被启用的组件

2-使用classname或链接文本是因为它可能出现在页面的其他位置,所以最好使用By.ID或By。XPath的

3 - 组件“退出”应该是在同一帧以前的组件

好运:-)

相关问题