Final commit to master merge from
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / US / MobProxy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.US;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.net.Proxy;
27 import java.net.ProxySelector;
28 import java.net.SocketAddress;
29 import java.net.URI;
30 import java.util.List;
31
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.WebDriver;
34 import org.openqa.selenium.WebElement;
35 import org.openqa.selenium.firefox.FirefoxDriver;
36 import org.openqa.selenium.remote.CapabilityType;
37 import org.openqa.selenium.remote.DesiredCapabilities;
38 import org.testng.AssertJUnit;
39 import org.testng.annotations.AfterClass;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.Test;
42
43 import com.github.markusbernhardt.proxy.ProxySearch;
44 import com.github.markusbernhardt.proxy.ProxySearch.Strategy;
45
46 import net.lightbody.bmp.BrowserMobProxyServer;
47 import net.lightbody.bmp.client.ClientUtil;
48 import net.lightbody.bmp.core.har.Har;
49 import net.lightbody.bmp.proxy.CaptureType;
50  
51 public class MobProxy {
52         public static WebDriver driver;
53         public static BrowserMobProxyServer server;
54  
55         @BeforeClass
56         public void setup() throws Exception {
57                 
58                 ProxySearch proxySearch = new ProxySearch();
59                 proxySearch.addStrategy(Strategy.OS_DEFAULT); 
60                 proxySearch.addStrategy(Strategy.JAVA); 
61                 proxySearch.addStrategy(Strategy.BROWSER); 
62                 ProxySelector proxySelector = proxySearch.getProxySelector(); 
63
64                 ProxySelector.setDefault(proxySelector); 
65                 URI home = URI.create("http://www.google.com"); 
66                 System.out.println("ProxySelector: " + proxySelector); 
67                 System.out.println("URI: " + home); 
68                 List<Proxy> proxyList = proxySelector.select(home); 
69                 String host = null;
70                 String port = null;
71                 if (proxyList != null && !proxyList.isEmpty()) { 
72                  for (Proxy proxy : proxyList) { 
73                    System.out.println(proxy); 
74                    SocketAddress address = proxy.address(); 
75                    if (address instanceof InetSocketAddress) { 
76                      host = ((InetSocketAddress) address).getHostName(); 
77                      port = Integer.toString(((InetSocketAddress) address).getPort()); 
78                      System.setProperty("http.proxyHost", host); 
79                      System.setProperty("http.proxyPort", port); 
80                    } 
81                  } 
82                 }
83                 
84                 server = new BrowserMobProxyServer();
85                 InetSocketAddress address = new InetSocketAddress(host, Integer.parseInt(port));
86             server.setChainedProxy(address);
87                 server.start();
88                 int port1 = server.getPort();
89                 DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
90                 seleniumCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
91                 seleniumCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(server));
92                 driver = new FirefoxDriver(seleniumCapabilities);
93                 server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
94                 System.out.println("Port started:" + port1);
95         }
96  
97         @Test
98         public void first_test1() throws InterruptedException {
99  
100                 server.newHar("asdc.har");
101  
102                 driver.get("https://www.e-access.att.com/QA-SCRUM1/sdc1/portal#/dashboard");
103                 driver.manage().window().maximize();
104                 
105                 WebElement userNameTextbox = driver.findElement(By.name("userid"));
106                 userNameTextbox.sendKeys("m99121");
107                 WebElement passwordTextbox = driver.findElement(By.name("password"));
108                 passwordTextbox.sendKeys("66-Percent");
109                 
110                 WebElement submitButton = driver.findElement(By.name("btnSubmit"));
111                 submitButton.click();
112                 Thread.sleep(300);
113                 WebElement buttonOK = driver.findElement(By.name("successOK"));
114                 AssertJUnit.assertTrue(buttonOK.isDisplayed());
115                 buttonOK.click();
116                 Thread.sleep(2000);
117                 driver.findElement(By.xpath(getXpath("main-menu-button-catalog"))).click();
118                 Thread.sleep(2000);             
119                 driver.findElement(By.xpath(getXpath("checkbox-service"))).click();
120                 Thread.sleep(2000);
121         }
122         
123         public static String getXpath(String dataTestId){
124                 return String.format("//*[@data-tests-id='%s']", dataTestId);
125         }
126  
127         @AfterClass
128         public void shutdown() {
129                 try {
130                 
131                         // Get the HAR data
132                         Har har = server.getHar();
133                         File harFile = new File("C:\\temp\\asdc.har");
134                         har.writeTo(harFile);
135  
136                 } catch (IOException ioe) {
137                         ioe.printStackTrace();
138                 }
139                 driver.quit();
140                 server.stop();
141         }
142 }