[SDC-29] rebase continue work to align source
[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.ProxySelector;
27 import java.net.SocketAddress;
28 import java.net.URI;
29 import java.util.List;
30
31 import java.net.Proxy;
32
33 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
34 import org.openqa.selenium.By;
35 import org.openqa.selenium.WebDriver;
36 import org.openqa.selenium.WebElement;
37 import org.openqa.selenium.firefox.FirefoxDriver;
38 import org.openqa.selenium.remote.CapabilityType;
39 import org.openqa.selenium.remote.DesiredCapabilities;
40 import org.testng.AssertJUnit;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 import com.github.markusbernhardt.proxy.ProxySearch;
46 import com.github.markusbernhardt.proxy.ProxySearch.Strategy;
47
48 import net.lightbody.bmp.BrowserMobProxyServer;
49 import net.lightbody.bmp.client.ClientUtil;
50 import net.lightbody.bmp.core.har.Har;
51 import net.lightbody.bmp.proxy.CaptureType;
52  
53 public class MobProxy {
54         public static WebDriver driver;
55         public static BrowserMobProxyServer server;
56  
57         @BeforeClass
58         public void setup() throws Exception {
59                 
60                 ProxySearch proxySearch = new ProxySearch();
61                 proxySearch.addStrategy(Strategy.OS_DEFAULT); 
62                 proxySearch.addStrategy(Strategy.JAVA); 
63                 proxySearch.addStrategy(Strategy.BROWSER); 
64                 ProxySelector proxySelector = proxySearch.getProxySelector(); 
65
66                 ProxySelector.setDefault(proxySelector); 
67                 URI home = URI.create("http://www.google.com"); 
68                 System.out.println("ProxySelector: " + proxySelector); 
69                 System.out.println("URI: " + home); 
70                 List<Proxy> proxyList = proxySelector.select(home); 
71                 String host = null;
72                 String port = null;
73                 if (proxyList != null && !proxyList.isEmpty()) { 
74                  for (Proxy proxy : proxyList) { 
75                    System.out.println(proxy); 
76                    SocketAddress address = proxy.address(); 
77                    if (address instanceof InetSocketAddress) { 
78                      host = ((InetSocketAddress) address).getHostName(); 
79                      port = Integer.toString(((InetSocketAddress) address).getPort()); 
80                      System.setProperty("http.proxyHost", host); 
81                      System.setProperty("http.proxyPort", port); 
82                    } 
83                  } 
84                 }
85                 
86                 server = new BrowserMobProxyServer();
87                 InetSocketAddress address = new InetSocketAddress(host, Integer.parseInt(port));
88             server.setChainedProxy(address);
89                 server.start();
90                 int port1 = server.getPort();
91                 DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
92                 seleniumCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
93                 seleniumCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(server));
94                 driver = new FirefoxDriver(seleniumCapabilities);
95                 server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
96                 System.out.println("Port started:" + port1);
97         }
98  
99         @Test
100         public void first_test1() throws InterruptedException {
101  
102                 server.newHar("asdc.har");
103  
104                 driver.get("https://www.e-access.att.com/QA-SCRUM1/sdc1/portal#/dashboard");
105                 driver.manage().window().maximize();
106                 
107                 WebElement userNameTextbox = driver.findElement(By.name("userid"));
108                 userNameTextbox.sendKeys("m99121");
109                 WebElement passwordTextbox = driver.findElement(By.name("password"));
110                 passwordTextbox.sendKeys("66-Percent");
111                 
112                 WebElement submitButton = driver.findElement(By.name("btnSubmit"));
113                 submitButton.click();
114                 Thread.sleep(300);
115                 WebElement buttonOK = driver.findElement(By.name("successOK"));
116                 AssertJUnit.assertTrue(buttonOK.isDisplayed());
117                 buttonOK.click();
118                 Thread.sleep(2000);
119                 driver.findElement(By.xpath(getXpath("main-menu-button-catalog"))).click();
120                 Thread.sleep(2000);             
121                 driver.findElement(By.xpath(getXpath("checkbox-service"))).click();
122                 Thread.sleep(2000);
123         }
124         
125         public static String getXpath(String dataTestId){
126                 return String.format("//*[@data-tests-id='%s']", dataTestId);
127         }
128  
129         @AfterClass
130         public void shutdown() {
131                 try {
132                 
133                         // Get the HAR data
134                         Har har = server.getHar();
135                         File harFile = new File("C:\\temp\\asdc.har");
136                         har.writeTo(harFile);
137  
138                 } catch (IOException ioe) {
139                         ioe.printStackTrace();
140                 }
141                 driver.quit();
142                 server.stop();
143         }
144 }