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