Selenium元素封装

  1 import org.ho.yaml.Yaml;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.util.HashMap;
  6 import org.openqa.selenium.By;
  7 import org.openqa.selenium.WebDriver;
  8 import org.openqa.selenium.WebElement;
  9 import org.openqa.selenium.firefox.*;
 10 import org.openqa.selenium.support.ui.ExpectedCondition;
 11 import org.openqa.selenium.support.ui.WebDriverWait;
 12 
 13 public class Demo {
 14 
 15     private String yamlFile;
 16     public WebDriver driver;
 17     
 18     public Demo() throws InterruptedException
 19     {
 20         System.setProperty ("webdriver.firefox.bin" , "D:/Program Files/Mozilla Firefox/firefox.exe");
 21         driver = new FirefoxDriver();
 22         driver.get("http://www.baidu.com");
 23         Thread.sleep(200);
 24         yamlFile = "demo";
 25         this.getYamlFile();
 26     }
 27     
 28     private HashMap<String, HashMap<String, String>> ml;
 29     
 30     public void getYamlFile()
 31     {
 32         File f = new File(yamlFile + ".yaml");
 33         try
 34         {
 35             ml = Yaml.loadType(new FileInputStream(f.getAbsolutePath()), 
 36                     HashMap.class);
 37         }
 38         catch(FileNotFoundException e)
 39         {
 40             e.printStackTrace();
 41         }
 42     }
 43     
 44     private By getBy(String type, String value) {
 45         By by = null;
 46         if (type.equals("id")) 
 47         {
 48             by = By.id(value);
 49         }
 50         if (type.equals("name"))
 51         {
 52             by = By.name(value);
 53         }
 54         if (type.equals("xpath")) 
 55         {
 56             by = By.xpath(value);
 57         }
 58         if (type.equals("className"))
 59         {
 60             by = By.className(value);
 61         }
 62         if (type.equals("linkText")) 
 63         {
 64             by = By.linkText(value);
 65         }
 66         return by;
 67     }
 68     
 69     private WebElement watiForElement(final By by) {
 70         WebElement element = null;
 71         int waitTime = Integer.parseInt("5");
 72         try 
 73         {
 74             element = new WebDriverWait(driver, waitTime)
 75                     .until(new ExpectedCondition<WebElement>() {
 76                         public WebElement apply(WebDriver d) {
 77                             return d.findElement(by);
 78                         }
 79                     });
 80         } 
 81         catch (Exception e) 
 82         {
 83             System.out.println(by.toString() + " is not exist until " + waitTime);
 84         }
 85         return element;
 86     }
 87     
 88     private boolean waitElementToBeDisplayed(final WebElement element) {
 89         boolean wait = false;
 90         if (element == null)
 91         {
 92             return wait;
 93         }
 94         try 
 95         {
 96             wait = new WebDriverWait(driver, Integer.parseInt("5"))
 97                     .until(new ExpectedCondition<Boolean>() {
 98                         public Boolean apply(WebDriver d) {
 99                             return element.isDisplayed();
100                         }
101                     });
102         } 
103         catch (Exception e)
104         {
105             System.out.println(element.toString() + " is not displayed");
106         }
107         return wait;
108     }
109     
110     public WebElement getElement(String key) {
111         String type = ml.get(key).get("type");
112         String value = ml.get(key).get("value");
113         WebElement element = this.watiForElement(this.getBy(type, value));
114         //if(!this.waitElementToBeDisplayed(element))
115         //    element = null;
116         return element;
117     }
118     
119     public static void main(String[] args) throws InterruptedException {
120         Demo d = new Demo();
121         d.getElement("baidu_input").sendKeys("test");
122         d.getElement("baidu_button").click();
123     }
124 
125 }

需要下载jyaml.jar包,用于解析yaml文件

ymal文件模板:

转载于:https://www.cnblogs.com/iamcodylee/p/3457284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值