查找简单行为,因此我在其中插入了以下值: package stackoverflow.proof.selenium; import java.util.Collection; import java.util.List; import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.junit.Assert; /** * Parameterized test which can be used to validate lookups for a single well-defined desired interaction in a Selenium test. * * @see "http://stackoverflow.com/questions/33151925/eclipse-java-selenium-webdriver-unable-to-select-dropdown-item-elementnotvisi/33167573#33167573" * @see "" * * @author http://stackoverflow.com/users/5407189/jeremiah */ @RunWith(Parameterized.class) public class SeleniumLookupValidationUtilTest { /** Test URL where the element should be validated.*/ private static final String TEST_URL = "http://www.smartclient.com/smartgwt/showcase/#featured_tile_filtering"; /** Function which converts a String into a {@link By#id(String)} reference.*/ private static final Function<String, By> AS_ID_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.id(arg0); } }; /** Function which converts a String into a {@link By#xpath(String)} reference.*/ private static final Function<String, By> AS_XPATH_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.xpath(arg0); } }; /** Function which converts a String into a {@link By#cssSelector(String)} reference.*/ private static final Function<String, By> AS_CSS_SELECTOR_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.cssSelector(arg0); } }; /** Function which converts a String into a {@link By#className(String)} reference.*/ private static final Function<String, By> AS_CLASS_NAME_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.className(arg0); } }; /** Function which converts a String into a {@link By#linkText(String)} reference.*/ private static final Function<String, By> AS_LINK_TEXT_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.linkText(arg0); } }; /** * Creates the data for running the test instance. * @return Collection of Object arrays. Each array in the collection represents a different test execution. */ @Parameters(name="{0}") public static Collection<Object[]> buildLookups() { String[] ids = new String[]{}; String[] xpaths = new String[]{".//*[@id='isc_PickListMenu_0_row_1']/td/div"}; String[] cssSelectors = new String[]{".pickListCellSelectedOverDark>div"}; String[] classNames = new String[]{}; String[] linkTexts = new String[]{}; //I use the map so I can loop through the dataset later. Map<Function<String, By>, String[]> associations = Maps.newHashMap(); associations.put(AS_ID_LOOKUP, ids); associations.put(AS_XPATH_LOOKUP,xpaths); associations.put(AS_CSS_SELECTOR_LOOKUP, cssSelectors); associations.put(AS_CLASS_NAME_LOOKUP, classNames); associations.put(AS_LINK_TEXT_LOOKUP, linkTexts); List<Object[]> parameters = Lists.newArrayList(); for (Function<String, By> converter: associations.keySet()) { String[] lookupStrings = associations.get(converter); for (String lookup : lookupStrings) { By by = converter.apply(lookup); parameters.add(new Object[]{by}); } } return parameters; } /** The By lookup to use for the current test validation.*/ private final By target; /** WebDriver for testing.*/ private WebDriver driver; /** * Test constructor. * @param lookup By to be used for this validation. */ public SeleniumLookupValidationUtilTest(By lookup) { this.target = lookup; } /** * Creates the webdriver and establishes our validation state. */ @Before public void initWebDriver() { driver = new FirefoxDriver(); driver.get(TEST_URL); } /** * Performs the thing we actually want to know about. */ @Test public void performValidation() { WebDriverWait wait = new WebDriverWait(driver, 15); //Wait for GWT to load WebElement dropDown = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='isc_2W']"))); //Find the drop down //Click the dropdown to expand the options dropDown.click(); //The element should now be availalbe to us, so try to resolve it. WebElement dropDownOption = wait.until(ExpectedConditions.elementToBeClickable(target)); //Validate that the lookup we got is the one we wanted. Assert.assertEquals("Expected 'Sort By' Dropdown option 'Life Span' not found by provided lookup.", "Life Span", dropDownOption.getText()); //Click it to select that option in the interface. dropDownOption.click(); //Verify the combo updated as expected. Assert.assertEquals("'Sort By' Dropdown option 'Life Span' is not selected", "Life Span", dropDown.getText()); } /** * Closes all test browser windows and destroys the driver reference. */ @After public void destroyWebDriver() { for (String window : driver.getWindowHandles()) { driver.switchTo().window(window); driver.close(); } } }
线程。睡眠通常被视为坏习惯,主要原因有两个。首先,在Selenium测试中,运行操作已经花费了很长时间。使用Thread.sleep会强制您始终等待配置的时间,默认情况下不会提前退出该行为。第二个原因是,即使睡眠确实退出,您也只能假设系统的状态是您想要的,但无法通过标准的在线实现来保证它 WebDriverWait是Selenium API中的首选替代方案。它将阻止您的测试继续进行,直到您正在寻找的条件有效。它在ExpectedConditions类中提供了一组很好的预打包选项,如果您发现需要定制自己的条件,它还利用了Google Guava API 为了解决时间问题,WebDriverWait将在两个条件之一下退出。等待条件的等待已过期,或者条件现在存在,测试可以继续。在后一种情况下,配置等待时间的长短无关紧要。一旦条件为真,等待释放,测试继续进行查找简单行为,因此我在其中插入了以下值: package stackoverflow.proof.selenium; import java.util.Collection; import java.util.List; import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.junit.Assert; /** * Parameterized test which can be used to validate lookups for a single well-defined desired interaction in a Selenium test. * * @see "http://stackoverflow.com/questions/33151925/eclipse-java-selenium-webdriver-unable-to-select-dropdown-item-elementnotvisi/33167573#33167573" * @see "" * * @author http://stackoverflow.com/users/5407189/jeremiah */ @RunWith(Parameterized.class) public class SeleniumLookupValidationUtilTest { /** Test URL where the element should be validated.*/ private static final String TEST_URL = "http://www.smartclient.com/smartgwt/showcase/#featured_tile_filtering"; /** Function which converts a String into a {@link By#id(String)} reference.*/ private static final Function<String, By> AS_ID_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.id(arg0); } }; /** Function which converts a String into a {@link By#xpath(String)} reference.*/ private static final Function<String, By> AS_XPATH_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.xpath(arg0); } }; /** Function which converts a String into a {@link By#cssSelector(String)} reference.*/ private static final Function<String, By> AS_CSS_SELECTOR_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.cssSelector(arg0); } }; /** Function which converts a String into a {@link By#className(String)} reference.*/ private static final Function<String, By> AS_CLASS_NAME_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.className(arg0); } }; /** Function which converts a String into a {@link By#linkText(String)} reference.*/ private static final Function<String, By> AS_LINK_TEXT_LOOKUP = new Function<String, By>() { public By apply(String arg0) { return By.linkText(arg0); } }; /** * Creates the data for running the test instance. * @return Collection of Object arrays. Each array in the collection represents a different test execution. */ @Parameters(name="{0}") public static Collection<Object[]> buildLookups() { String[] ids = new String[]{}; String[] xpaths = new String[]{".//*[@id='isc_PickListMenu_0_row_1']/td/div"}; String[] cssSelectors = new String[]{".pickListCellSelectedOverDark>div"}; String[] classNames = new String[]{}; String[] linkTexts = new String[]{}; //I use the map so I can loop through the dataset later. Map<Function<String, By>, String[]> associations = Maps.newHashMap(); associations.put(AS_ID_LOOKUP, ids); associations.put(AS_XPATH_LOOKUP,xpaths); associations.put(AS_CSS_SELECTOR_LOOKUP, cssSelectors); associations.put(AS_CLASS_NAME_LOOKUP, classNames); associations.put(AS_LINK_TEXT_LOOKUP, linkTexts); List<Object[]> parameters = Lists.newArrayList(); for (Function<String, By> converter: associations.keySet()) { String[] lookupStrings = associations.get(converter); for (String lookup : lookupStrings) { By by = converter.apply(lookup); parameters.add(new Object[]{by}); } } return parameters; } /** The By lookup to use for the current test validation.*/ private final By target; /** WebDriver for testing.*/ private WebDriver driver; /** * Test constructor. * @param lookup By to be used for this validation. */ public SeleniumLookupValidationUtilTest(By lookup) { this.target = lookup; } /** * Creates the webdriver and establishes our validation state. */ @Before public void initWebDriver() { driver = new FirefoxDriver(); driver.get(TEST_URL); } /** * Performs the thing we actually want to know about. */ @Test public void performValidation() { WebDriverWait wait = new WebDriverWait(driver, 15); //Wait for GWT to load WebElement dropDown = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='isc_2W']"))); //Find the drop down //Click the dropdown to expand the options dropDown.click(); //The element should now be availalbe to us, so try to resolve it. WebElement dropDownOption = wait.until(ExpectedConditions.elementToBeClickable(target)); //Validate that the lookup we got is the one we wanted. Assert.assertEquals("Expected 'Sort By' Dropdown option 'Life Span' not found by provided lookup.", "Life Span", dropDownOption.getText()); //Click it to select that option in the interface. dropDownOption.click(); //Verify the combo updated as expected. Assert.assertEquals("'Sort By' Dropdown option 'Life Span' is not selected", "Life Span", dropDown.getText()); } /** * Closes all test browser windows and destroys the driver reference. */ @After public void destroyWebDriver() { for (String window : driver.getWindowHandles()) { driver.switchTo().window(window); driver.close(); } } },java,selenium,drop-down-menu,combobox,Java,Selenium,Drop Down Menu,Combobox,线程。睡眠通常被视为坏习惯,主要原因有两个。首先,在Selenium测试中,运行操作已经花费了很长时间。使用Thread.sleep会强制您始终等待配置的时间,默认情况下不会提前退出该行为。第二个原因是,即使睡眠确实退出,您也只能假设系统的状态是您想要的,但无法通过标准的在线实现来保证它 WebDriverWait是Selenium API中的首选替代方案。它将阻止您的测试继续进行,直到您正在寻找的条件有效。它在ExpectedConditions类中提供了一组很好的预打包选项,如果您发现需要定
祝你好运 同时,我在Selenium IDE中发现了一些允许使用所谓“scLocators”的东西。他们似乎工作得很好。现在我正试图让它们在SeleniumWebDriver中工作。同时,我发现了一些允许在SeleniumIDE中使用所谓的“scLocators”的东西。他们似乎工作得很好。现在我正试图让它们在SeleniumWebDriver中工作。
<tr aria-selected="true" id="isc_PickListMenu_0_row_0" role="option" aria-setsize="3" aria-posinset="1">
<td style="padding-top: 0px; padding-bottom: 0px; width: 144px; overflow: hidden;" class="pickListCellSelected" align="left" height="16">
<div role="presentation" cellclipdiv="true" style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;WIDTH:140px;">Animal</div>
</td>
</tr>
<tr aria-selected="false" id="isc_PickListMenu_0_row_1" role="option" aria-setsize="3" aria-posinset="2">
<td style="padding-top: 0px; padding-bottom: 0px; width: 144px; overflow: hidden;" class="pickListCellDark" align="left" height="16">
<div role="presentation" cellclipdiv="true" style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;WIDTH:140px;">Life Span</div>
</td>
</tr>
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.smartclient.com/smartgwt/showcase/#featured_tile_filtering");
driver.findElement(By.id("isc_2W")).click();
driver.findElement(By.xpath("//div[.='Life Span']")).click();
driver.get("http://www.smartclient.com/smartgwt/showcase/#featured_tile_filtering");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='isc_2Z']")).click();
driver.findElement(By.xpath("//*[@id='isc_PickListMenu_0_row_1']/td/div")).click();
Thread.sleep(2000);
package stackoverflow.proof.selenium;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Assert;
/**
* Parameterized test which can be used to validate lookups for a single well-defined desired interaction in a Selenium test.
*
* @see "http://stackoverflow.com/questions/33151925/eclipse-java-selenium-webdriver-unable-to-select-dropdown-item-elementnotvisi/33167573#33167573"
* @see ""
*
* @author http://stackoverflow.com/users/5407189/jeremiah
*/
@RunWith(Parameterized.class)
public class SeleniumLookupValidationUtilTest {
/** Test URL where the element should be validated.*/
private static final String TEST_URL = "http://www.smartclient.com/smartgwt/showcase/#featured_tile_filtering";
/** Function which converts a String into a {@link By#id(String)} reference.*/
private static final Function<String, By> AS_ID_LOOKUP = new Function<String, By>() {
public By apply(String arg0) {
return By.id(arg0);
}
};
/** Function which converts a String into a {@link By#xpath(String)} reference.*/
private static final Function<String, By> AS_XPATH_LOOKUP = new Function<String, By>() {
public By apply(String arg0) {
return By.xpath(arg0);
}
};
/** Function which converts a String into a {@link By#cssSelector(String)} reference.*/
private static final Function<String, By> AS_CSS_SELECTOR_LOOKUP = new Function<String, By>() {
public By apply(String arg0) {
return By.cssSelector(arg0);
}
};
/** Function which converts a String into a {@link By#className(String)} reference.*/
private static final Function<String, By> AS_CLASS_NAME_LOOKUP = new Function<String, By>() {
public By apply(String arg0) {
return By.className(arg0);
}
};
/** Function which converts a String into a {@link By#linkText(String)} reference.*/
private static final Function<String, By> AS_LINK_TEXT_LOOKUP = new Function<String, By>() {
public By apply(String arg0) {
return By.linkText(arg0);
}
};
/**
* Creates the data for running the test instance.
* @return Collection of Object arrays. Each array in the collection represents a different test execution.
*/
@Parameters(name="{0}")
public static Collection<Object[]> buildLookups() {
String[] ids = new String[]{};
String[] xpaths = new String[]{".//*[@id='isc_PickListMenu_0_row_1']/td/div"};
String[] cssSelectors = new String[]{".pickListCellSelectedOverDark>div"};
String[] classNames = new String[]{};
String[] linkTexts = new String[]{};
//I use the map so I can loop through the dataset later.
Map<Function<String, By>, String[]> associations = Maps.newHashMap();
associations.put(AS_ID_LOOKUP, ids);
associations.put(AS_XPATH_LOOKUP,xpaths);
associations.put(AS_CSS_SELECTOR_LOOKUP, cssSelectors);
associations.put(AS_CLASS_NAME_LOOKUP, classNames);
associations.put(AS_LINK_TEXT_LOOKUP, linkTexts);
List<Object[]> parameters = Lists.newArrayList();
for (Function<String, By> converter: associations.keySet()) {
String[] lookupStrings = associations.get(converter);
for (String lookup : lookupStrings) {
By by = converter.apply(lookup);
parameters.add(new Object[]{by});
}
}
return parameters;
}
/** The By lookup to use for the current test validation.*/
private final By target;
/** WebDriver for testing.*/
private WebDriver driver;
/**
* Test constructor.
* @param lookup By to be used for this validation.
*/
public SeleniumLookupValidationUtilTest(By lookup) {
this.target = lookup;
}
/**
* Creates the webdriver and establishes our validation state.
*/
@Before
public void initWebDriver() {
driver = new FirefoxDriver();
driver.get(TEST_URL);
}
/**
* Performs the thing we actually want to know about.
*/
@Test
public void performValidation() {
WebDriverWait wait = new WebDriverWait(driver, 15);
//Wait for GWT to load
WebElement dropDown = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='isc_2W']"))); //Find the drop down
//Click the dropdown to expand the options
dropDown.click();
//The element should now be availalbe to us, so try to resolve it.
WebElement dropDownOption = wait.until(ExpectedConditions.elementToBeClickable(target));
//Validate that the lookup we got is the one we wanted.
Assert.assertEquals("Expected 'Sort By' Dropdown option 'Life Span' not found by provided lookup.", "Life Span", dropDownOption.getText());
//Click it to select that option in the interface.
dropDownOption.click();
//Verify the combo updated as expected.
Assert.assertEquals("'Sort By' Dropdown option 'Life Span' is not selected", "Life Span", dropDown.getText());
}
/**
* Closes all test browser windows and destroys the driver reference.
*/
@After
public void destroyWebDriver() {
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
driver.close();
}
}
}
/**
* Performs the thing we actually want to know about.
*/
@Test
public void performValidation() {
WebDriverWait wait = new WebDriverWait(driver, 15);
//Wait for GWT to load
WebElement dropDown = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='isc_2W']"))); //Find the drop down
//Click the dropdown to expand the options
dropDown.click();
//The element should now be availalbe to us, so try to resolve it.
WebElement dropDownOption = wait.until(ExpectedConditions.elementToBeClickable(target));
//Validate that the lookup we got is the one we wanted.
Assert.assertEquals("Expected 'Sort By' Dropdown option 'Life Span' not found by provided lookup.", "Life Span", dropDownOption.getText());
//Click it to select that option in the interface.
dropDownOption.click();
//Verify the combo updated as expected.
Assert.assertEquals("'Sort By' Dropdown option 'Life Span' is not selected", "Life Span", dropDown.getText());
}