Я оцениваю Selenium2 для проекта в Internet Explorer 9 и выбрал веб-сайт mercedes-benz, потому что он использует AJAX и некоторые встроенные всплывающие окна, поэтому я написал небольшой тест.
- goto mercedes-benz.ch
- нажмите на «A», затем выберите «Konfigurator» в отображаемом оверлее
- нажмите на "Weiter>"
- нажмите на "> Оценить ändern", и появится всплывающее окно.
- нажмите кнопку «i» во всплывающем окне
В моем примере кода есть 2 проблемы:
1-й: Как создать цепочку действий с динамическим контентом, который еще не доступен при создании цепочки
(т.е. moveToElement (A) .moveToElement (B): A делает B видимым, поэтому при построении B не существует)
2nd: это img-элемент, заключенный в a-элемент, который я хочу щелкнуть:
<a onclick="var cf = function(){ openInfo('InsurancePpi'); return false;};
var oamSF = function(){ return oamSubmitForm('calcForm','calcForm:j_id31');};
return (cf()==false) ? false : oamSF();" href="#">
<img src="images/mb/btn_info_rb.gif">
</a>
но .click () ничего не делает. Кажется, что щелчок выполнен, но ничего не происходит там, где должно быть показано всплывающее окно. Если я использую .sendKeys (Keys.Enter) вместо .click (), он работает просто отлично.
мой пример кода:
package org.test.demo;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
@Test
public class TestStep_DebugChangeRate {
public void test() {
driver.manage().deleteAllCookies();
driver.get("http://www.mercedes-benz.ch");
WebElement btnA = driver.findElement(By.xpath("//a[.='A']"));
// first problem, i cannot combine moveToElement(btnA) and
// moveToElement(btnKonfigurator), because btnKonfigurator is not
// visible at the moment of building the Action which will then fail
// in NoSuchElement, that's why I cheat with sendKeys(), any tips?
(new Actions(driver))
.moveToElement(btnA)
.click(btnA)
.sendKeys(Keys.TAB)
.sendKeys(Keys.TAB)
.sendKeys(Keys.TAB)
.sendKeys(Keys.ENTER)
.build()
.perform();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement btnContinue = driver.findElement(By.id("vsAppLnkContinue1"));
btnContinue.click();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement btnChangeRate = driver.findElement(By.id("vsAppLnkPcnChangeRate"));
btnChangeRate.click();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement frameInline = driver.findElement(By.xpath("//div[@class='contentsC']/iframe"));
WebDriver frame = driver.switchTo().frame(frameInline);
WebElement btnInfoPpi = frame.findElement(By.xpath("//a[contains(@onclick, 'InsurancePpi')]/img"));
// does not throw an error, though the info popup is not opened
btnInfoPpi.click();
Boolean isDisplayed = btnInfoPpi.isDisplayed(); // true
int elementWidth = btnInfoPpi.getSize().getWidth(); // 23
btnInfoPpi.sendKeys(Keys.Enter); // this will open the popup however
}
}