Blame view

src/test/java/com/essa/framework/BrowserEngine.java 4.36 KB
fdd4bb76   zengjin55   windows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  package com.essa.framework;
  
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Properties;
  import java.util.concurrent.TimeUnit;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.firefox.FirefoxProfile;
  import org.openqa.selenium.ie.InternetExplorerDriver;
  import org.openqa.selenium.remote.CapabilityType;
  import org.openqa.selenium.remote.DesiredCapabilities;
  
  public class BrowserEngine {  
      
      private String browserName;  
      private String serverURL;  
      private WebDriver driver;  
        
      public void initConfigData() throws IOException{  
            
          Properties p = new Properties();  
          // 加载配置文件  
          InputStream ips = new FileInputStream(".\\src\\main\\resources\\TestConfig\\config.properties");  
          p.load(ips);  
            
          Logger.Output(LogType.LogTypeName.INFO, "开始从配置文件中选择浏览器");  
          browserName=p.getProperty("browserName");  
          Logger.Output(LogType.LogTypeName.INFO, "所选择的浏览器类型为: "+ browserName);  
          serverURL = p.getProperty("URL");  
          Logger.Output(LogType.LogTypeName.INFO, "所测试的URL地址为: "+ serverURL);  
          ips.close();  
      }  
        
      public WebDriver getBrowser(){  
          
      	if(browserName.equalsIgnoreCase("Firefox")){  
          	
              System.setProperty("webdriver.gecko.driver", ".\\src\\main\\resources\\geckodriver.exe");    
              driver = createFireFoxDriver();  
              Logger.Output(LogType.LogTypeName.INFO, "正在启动FireFox浏览器");  
                
          }
      	else if(browserName.equals("Chrome")){  
              System.setProperty("webdriver.chrome.driver", ".\\src\\main\\resources\\chromedriver.exe");  
              driver= new ChromeDriver();  
              Logger.Output(LogType.LogTypeName.INFO, "正在启动Chrome浏览器");  
                
          }else if(browserName.equalsIgnoreCase("IE")){  
                
              System.setProperty("webdriver.ie.driver", ".\\src\\main\resources\\IEDriverServer.exe");  
              driver= new InternetExplorerDriver();  
              Logger.Output(LogType.LogTypeName.INFO, "正在启动IE浏览器");  
          }
      	driver.manage().window().maximize();  
          Logger.Output(LogType.LogTypeName.INFO, "窗口最大化");
          driver.get(serverURL);  
          Logger.Output(LogType.LogTypeName.INFO, "打开URL: "+ serverURL);  
          callWait(5);  
          return driver;  
      }  
        
      /* 
       * 关闭浏览器并退出方法 
       */  
        
      public void tearDown() throws InterruptedException{  
            
          Logger.Output(LogType.LogTypeName.INFO, "关闭浏览器");  
          driver.quit();  
          Thread.sleep(3000);  
      }  
        
      /* 
       * 隐式时间等待方法 
       */  
      public void callWait(int time){  
            
          driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);  
          Logger.Output(LogType.LogTypeName.INFO, "设置隐性等待"+time+" 秒");  
      }  
        
        
      /* 
       * createFireFox Driver 
       * @Param: null 
       * @return: WebDriver 
       */  
        
      private WebDriver createFireFoxDriver() {  
            
          WebDriver driver = null;  
          FirefoxProfile firefoxProfile = new FirefoxProfile();  
    
          firefoxProfile.setPreference("prefs.converted-to-utf8", true);  
          //set download folder to default folder: TestDownload  
          firefoxProfile.setPreference("browser.download.folderList", 2);  
          firefoxProfile.setPreference("browser.download.dir", ".\\TestDownload");  
            
          try {  
                  driver = new FirefoxDriver();  
          } catch (Exception e) {  
              Logger.Output(LogType.LogTypeName.ERROR, e.getMessage());  
              Logger.Output(LogType.LogTypeName.ERROR, "Failed to initilize the Firefox driver");  
          }  
          return driver;  
      }  
      public  void anQuan() {
      	 // 创建DesiredCapabilities类的一个对象实例  
          DesiredCapabilities cap=DesiredCapabilities.chrome();  
          // 设置变量ACCEPT_SSL_CERTS的值为True  
          cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          Logger.Output(LogType.LogTypeName.INFO, "设置浏览器可以打开不安全链接");
      }
  }