如何使用Haskell和webdriver从下拉列表中快速选择一个选项?

时间:2016-09-14 19:03:06

标签: selenium haskell webdriver

我有一个<select>的网站,其中包含超过100 <option>个网站,其中一个网站适用于世界上每个国家/地区。我的测试需要选择一个。我目前的解决方案 - 下面 - 大约需要2.5秒。我希望它快得多。

findChildrenByLabel :: (WD.WebDriver m, MonadIO m) =>
  Text -> Text -> m [WD.Element]
findChildrenByLabel label tagName = do
  parentEl <- findFieldByLabel label
  WD.findElemsFrom parentEl $ WD.ByTag tagName

findChildByLabelF :: (WD.WebDriver m, MonadIO m) =>
  Text -> Text -> (WD.Element -> m Bool) -> m WD.Element
findChildByLabelF label tagName f = do
  children <- findChildrenByLabel label tagName
  child <- filterM f children
  liftIO $ child `shouldSatisfy` ((==1) . length)
  return (headEx child)

selectDropdownByLabel :: (WD.WebDriver m, MonadIO m) => Text -> Text -> m ()
selectDropdownByLabel label optionName = do
  targetOption <- findChildByLabelF
    label "option" (\el -> (== optionName) <$> WD.getText el)
  WD.click targetOption

我在Java中找到了this SO answer,但似乎没有像Haskell那样的Selenium.SupportSelectByText

1 个答案:

答案 0 :(得分:0)

根据danidiaz的回答,我写了这个:

findFieldByLabel :: (WD.WebDriver m, MonadIO m) => Text -> m WD.Element
findFieldByLabel str = do
  labels :: [WD.Element] <- WD.findElems $ WD.ByXPath $
    "//label[text() = \"" <> str <> "\"]"
  els <- catMaybes <$> mapM (`WD.attr` "for") labels
  liftIO $ els `shouldSatisfy` ((==1) . length)
  WD.findElem $ WD.ById $ headEx els

selectDropdownByLabel :: (WD.WebDriver m, MonadIO m) => Text -> Text -> m ()
selectDropdownByLabel label optionName = do
  field <- findFieldByLabel label
  targetOptions <- WD.findElemsFrom field $ WD.ByXPath $
    "option[text() = \"" <> optionName <> "\"]"
  liftIO $ targetOptions `shouldSatisfy` ((==1) . length)
  WD.click $ headEx targetOptions

我这样用:

selectDropdownByLabel "Country of residence" "United States"

原始解决方案耗时约2.5秒,约为0.16。很大的改进,谢谢danidiaz!