我在Firefox中使用testng + selenium webdriver時遇到了奇怪的問題。 我有一個與數據提供者一起工作的測試。第一次測試運行正常。在測試結束後,我導航到上一頁,以使第二次運行正常。但在第二次運行WebElement.click()打開windown在新標籤而不是相同的標籤。 這就是爲什麼webdriver找不到其他元素。請給出任何建議如何解決這個問題!我試過: 1.使用Thread.Sleep。認爲它會有所幫助,但睡眠時間後,新的ta仍然打開 2.使用windows.Handle切換到新選項卡。它也沒有幫我,要素仍然沒有找到WebElement.click()打開新標籤,而不是在同一個標籤中工作
我的代碼如下(數據提供程序使用XML薩克斯獲取數據,這正常工作):
package com.epam.training.selenium;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.ParserConfigurationException;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import utils.TestingUtils;
import xmlparser.TestingData;
public class SeleniumTest
{
private WebDriver driver;
private String mainPage = "https://mail.ru";
private String inboxPage = "https://e.mail.ru/messages/inbox/?back=1";
private String loginVal = "seleniumtest";
private String passwordVal = "Qwerty123";
private String domainVal = "@inbox.ru";
@BeforeClass
public void startBrowser(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(mainPage);
}
@Test(groups = "smoke" , priority=1)
public void authTest(){
WebElement login = driver.findElement(By.xpath(".//*[@id='mailbox__login']"));
login.sendKeys(loginVal);
WebElement password = driver.findElement(By.xpath(".//*[@id='mailbox__password']"));
password.sendKeys(passwordVal);
Select selectDomain = new Select(driver.findElement(By.xpath(".//*[@id='mailbox__login__domain']")));
selectDomain.selectByVisibleText(domainVal);
WebElement authButton = driver.findElement(By.xpath(".//*[@id='mailbox__auth__button']"));
authButton.click();
WebElement logoutLink = driver.findElement(By.xpath(".//*[@id='PH_logoutLink']"));
boolean expected = true;
Assert.assertEquals(logoutLink.isDisplayed(), expected);
}
@Test(groups = {"drafts test"}, dependsOnGroups = "smoke", dataProvider = "New letter test data")
public void createNewLetter(TestingData data){
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement createLetterButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='b-toolbar__left']/div/div/div[2]/div/a")));
createLetterButton.click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
WebElement senderAdress = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='compose__header__content']/div[2]/div[2]/div[1]/textarea[2]")));
senderAdress.sendKeys(data.getSenderAdress());
WebElement senderSubject = driver.findElement(By.xpath(".//*[@class='compose__header__field']"));
Actions builder = new Actions(driver);
builder.sendKeys(senderSubject, data.getSenderSubject()).click().perform();
WebElement disableInteractionsButton = driver.findElement(By.xpath("html/body/div[2]/div/div[5]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[8]/div[2]/div[2]/div[2]/div/div/form/div[2]/div[4]/div[1]/div[2]/table/tbody/tr/td/table[1]/tbody/tr/td[19]/a/span[1]/span[2]"));
disableInteractionsButton.click();
WebElement senderText = driver.findElement(By.xpath("html/body/div[2]/div/div[5]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[8]/div[2]/div[2]/div[2]/div/div/form/div[2]/div[4]/div[3]/table/tbody/tr/td[1]/div/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[2]/td/textarea"));
builder.moveToElement(senderText).click().keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0061'))
.keyUp(Keys.CONTROL).sendKeys(data.getSenderText()).keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0073')).perform();
boolean expected = true;
// Assert.assertEquals(senderAdress.isDisplayed(), expected);
WebElement draftsLink = wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(".//div[@id='b-toolbar__right']/div/div/div[2]/div[6]/div"));
}
});
expected = true;
Assert.assertEquals(draftsLink.isEnabled(), expected);
driver.get(inboxPage);
try {
Alert alert = driver.switchTo().alert();
alert.accept();
}
catch (NoAlertPresentException e) {
}
}
@DataProvider(name = "New letter test data")
public Object[][] getValuesForNewDraft() throws ParserConfigurationException, SAXException, IOException{
List<TestingData> dataList = TestingUtils.parse();
return new Object[][]{
new Object[] {dataList.get(0)},
new Object[] {dataList.get(1)}
};
}
}
我的意思是這個WebElement createLetterButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(「.//*[@ id ='b-toolbar__left']/div/div/div [2]/div/a 「))); createLetterButton.click();它是一個網站上的按鈕 –
什麼是按鈕處理程序(js函數)? –
我不知道,就我所見,只有一個鏈接 –