Best SeLion code snippet using com.paypal.selion.grid.servlets.SauceConfigChangeServlet
Source:SauceConfigChangeServlet.java
...37/**38 * This {@link RegistryBasedServlet} based servlet updates the Sauce Configuration json file based on the input provided39 * via POST operation and re-loads the SauceConfigReader properties. For GET request it will return the40 * updateSauceConfigPage.html content. URL of the Servlet :41 * <code>http://{hub-host}:{hub-port}/grid/admin/SauceConfigChangeServlet</code>. <br>42 * <br>43 * This requires the hub to also have {@link LoginServlet} available.44 */45public class SauceConfigChangeServlet extends RegistryBasedServlet {46 private static final long serialVersionUID = 1L;47 private static final SeLionGridLogger LOGGER = SeLionGridLogger.getLogger(SauceConfigChangeServlet.class);48 /**49 * Resource path to the sauce config html file50 */51 public static final String RESOURCE_PAGE_FILE = "/com/paypal/selion/html/updateSauceConfigPage.html";52 /**53 * Form parameter for sauce url54 */55 public static final String SAUCE_URL_PARAM = "sauceURL";56 /**57 * Form parameter for the sauce username58 */59 public static final String USERNAME_PARAM = "username";60 /**61 * Form parameter for retry count on errors communicating with sauce api62 */63 public static final String RETRY_PARAM = "retry";64 /**65 * Form parameter for timeout when communicating with sauce api66 */67 public static final String TIMEOUT_PARAM = "timeout";68 /**69 * Form parameter for the sauce access key70 */71 public static final String ACCESS_KEY_PARAM = "accessKey";72 /**73 * Form parameter for the sauce parent tunnel74 */75 public static final String PARENT_TUNNEL_PARAM = "parentTunnel";76 /**77 * Form parameter for the sauce tunnel identifier78 */79 public static final String TUNNEL_IDENTIFIER_PARAM = "tunnelIdentifier";80 /**81 * Form parameter for require user supplied sauce credentials82 */83 public static final String REQUIRE_USER_CREDENTIALS_PARAM = "requireUserCredentials";84 public SauceConfigChangeServlet(GridRegistry registry) {85 super(registry);86 }87 public SauceConfigChangeServlet() {88 this(null);89 }90 @Override91 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {92 if (req.getSession(false) == null) {93 resp.sendRedirect(LoginServlet.class.getSimpleName());94 return;95 }96 loadSauceConfigPage(resp);97 }98 private void loadSauceConfigPage(HttpServletResponse resp) throws IOException {99 ServletHelper.respondAsHtmlUsingJsonAndTemplateWithHttpStatus(resp,100 JSONConfigurationUtils.loadJSON(SeLionGridConstants.SAUCE_CONFIG_FILE), RESOURCE_PAGE_FILE,101 HttpServletResponse.SC_OK);...
Source:SauceConfigChangeServletTest.java
...20import org.testng.annotations.BeforeClass;21import org.testng.annotations.Test;22import static org.testng.Assert.assertEquals;23import static org.testng.Assert.assertTrue;24public class SauceConfigChangeServletTest extends BaseGridRegistyServletTest {25 private static SauceConfigChangeServlet servlet;26 @BeforeClass(alwaysRun = true)27 public void beforeClass() {28 initRegistry();29 // Initialize the servlet under test30 servlet = new SauceConfigChangeServlet(registry);31 }32 /*33 * HTTP GET with no session. Should be redirected to login page34 */35 @Test36 public void testDoGetForRedirectToLoginServlet() throws Exception {37 MockHttpServletRequest request = new MockHttpServletRequest();38 MockHttpServletResponse response = new MockHttpServletResponse();39 servlet.doGet(request, response);40 validateRedirectedToLoginServlet(response);41 }42 /*43 * HTTP POST with no session. Should be redirected to login page44 */45 @Test46 public void testDoPostForRedirectToLoginServlet() throws Exception {47 MockHttpServletRequest request = new MockHttpServletRequest();48 MockHttpServletResponse response = new MockHttpServletResponse();49 servlet.doPost(request, response);50 validateRedirectedToLoginServlet(response);51 }52 /*53 * Config change form should be displayed54 */55 @Test56 public void testDoGetForConfigChangePage() throws Exception {57 MockHttpServletRequest request = new MockHttpServletRequest();58 request.getSession(true);59 MockHttpServletResponse response = new MockHttpServletResponse();60 servlet.doGet(request, response);61 validateHtmlResponseContent(response, "SeLion Grid - Sauce Proxy Configuration", "Sauce URL");62 }63 /*64 * Sauce config should be updated and config change success page displayed65 */66 @Test67 public void testDoPostForSuccess() throws Exception {68 MockHttpServletRequest request = new MockHttpServletRequest();69 request.getSession(true);70 request.addParameter(SauceConfigChangeServlet.SAUCE_URL_PARAM, "http://sauce-url");71 request.addParameter(SauceConfigChangeServlet.ACCESS_KEY_PARAM, "access-key");72 request.addParameter(SauceConfigChangeServlet.USERNAME_PARAM, "sauce-super-user");73 request.addParameter(SauceConfigChangeServlet.RETRY_PARAM, "1");74 request.addParameter(SauceConfigChangeServlet.TIMEOUT_PARAM, "1000");75 request.addParameter(SauceConfigChangeServlet.PARENT_TUNNEL_PARAM, "my-parent-tunnel");76 request.addParameter(SauceConfigChangeServlet.TUNNEL_IDENTIFIER_PARAM, "tunnel-100");77 request.addParameter(SauceConfigChangeServlet.REQUIRE_USER_CREDENTIALS_PARAM, "on");78 MockHttpServletResponse response = new MockHttpServletResponse();79 servlet.doPost(request, response);80 validateHtmlResponseContent(response, "Grid Management Console", "Sauce configuration updated successfully");81 SauceConfigReader reader = SauceConfigReader.getInstance();82 assertEquals(reader.getSauceURL(), "http://sauce-url");83 assertEquals(reader.getUserName(), "sauce-super-user");84 assertTrue(StringUtils.isNotBlank(reader.getAuthenticationKey()));85 assertEquals(reader.getSauceRetry(), 1);86 assertEquals(reader.getSauceTimeout(), 1000);87 assertEquals(reader.getDefaultParentTunnel(), "my-parent-tunnel");88 assertEquals(reader.getDefaultTunnelIdentifier(), "tunnel-100");89 assertTrue(reader.isRequireUserCredentials());90 }91}...
SauceConfigChangeServlet
Using AI Code Generation
1import com.paypal.selion.grid.servlets.SauceConfigChangeServlet;2import org.openqa.grid.web.servlet.RegistryBasedServlet;3import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;4import org.openqa.grid.web.servlet.handler.SeleniumBasedResponse;5public class SauceConfigChangeServlet extends RegistryBasedServlet {6 public SauceConfigChangeServlet() {7 super(null);8 }9 protected void doGet(SeleniumBasedRequest request, SeleniumBasedResponse response) throws Exception {10 response.setContentType("text/html");11 response.getWriter().println("<h2>Sauce Config Change Servlet</h2>");12 response.getWriter().println("The servlet is working");13 }14}15import org.openqa.grid.internal.Registry;16import org.openqa.grid.internal.RemoteProxy;17import org.openqa.grid.internal.TestSession;18import org.openqa.grid.web.servlet.RegistryBasedServlet;19import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;20import org.openqa.grid.web.servlet.handler.SeleniumBasedResponse;21import org.openqa.selenium.remote.CapabilityType;22import org.openqa.selenium.remote.DesiredCapabilities;23import org.openqa.selenium.remote.server.handler.GetCurrentUrl;24import java.io.IOException;25import java.util.Map;26public class SauceConfigChangeServlet extends RegistryBasedServlet {27 public SauceConfigChangeServlet() {28 super(null);29 }30 protected void doGet(SeleniumBasedRequest request, SeleniumBasedResponse response) throws Exception {31 response.setContentType("text/html");32 response.getWriter().println("<h2>Sauce Config Change Servlet</h2>");33 response.getWriter().println("The servlet is working");34 }35 protected void doPost(SeleniumBasedRequest request, SeleniumBasedResponse response) throws Exception {36 response.setContentType("text/html");37 response.getWriter().println("<h2>Sauce Config Change Servlet</h2>");38 response.getWriter().println("The servlet is working");39 String browser = request.getParameter("browser");40 String version = request.getParameter("version");41 String platform = request.getParameter("platform");42 String maxInstances = request.getParameter("maxInstances");43 String build = request.getParameter("build");44 Registry registry = getRegistry();45 int max = 0;46 if (maxInstances != null) {47 max = Integer.parseInt(maxInstances);48 }49 for (RemoteProxy proxy : registry.getAllProxies()) {50 if (proxy.getRemoteHost().getHost().contains("ondemand.saucelabs.com")) {
SauceConfigChangeServlet
Using AI Code Generation
1package com.paypal.selion.examples;2import java.io.BufferedReader;3import java.io.IOException;4import java.io.InputStreamReader;5import java.net.HttpURLConnection;6import java.net.URL;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.support.FindBy;10import org.openqa.selenium.support.PageFactory;11import org.testng.annotations.Test;12import com.paypal.selion.annotations.WebTest;13import com.paypal.selion.platform.grid.Grid;14import com.paypal.selion.platform.grid.browsercapabilities.DefaultCapabilitiesBuilder;15import com.paypal.selion.platform.grid.browsercapabilities.SauceCapabilitiesBuilder;16import com.paypal.selion.platform.grid.browsercapabilities.SauceLabsCapabilityMatcher;17import com.paypal.selion.platform.grid.browsercapabilities.SauceLabsCapabilities;18import com.paypal.selion.platform.grid.browsercapabilities.SauceLabsCapabilityMatcher.SauceLabsCapabilityType;19import com.paypal.selion.platform.html.Button;20import com.paypal.selion.platform.html.TextField;21import com.paypal.selion.platform.utilities.WebDriverWaitUtils;22public class SauceConfigChangeServletTest {23 @FindBy(id = "q")24 private WebElement searchBox;25 @FindBy(id = "submit")26 private WebElement submitButton;27 public void testSauceConfigChangeServlet() throws IOException {28 String serverUrl = Grid.getTestSession().getServerUrl();29 URL url = new URL(serverUrl + "/grid/admin/SauceConfigChangeServlet?username=yourUserName&accessKey=yourAccessKey");30 HttpURLConnection conn = (HttpURLConnection) url.openConnection();31 conn.setRequestMethod("GET");32 conn.setRequestProperty("Accept", "application/json");33 BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));34 String output;35 while ((output = br.readLine()) != null) {36 System.out.println(output);37 }38 conn.disconnect();39 WebDriver driver = Grid.driver(new DefaultCapabilitiesBuilder(new SauceCapabilitiesBuilder(40 new SauceLabsCapabilities(), new SauceLabsCapabilityMatcher(SauceLabsCapabilityType.DESKTOP))));41 PageFactory.initElements(driver, this);42 TextField search = new TextField(searchBox);43 search.type("Selenium");44 Button submit = new Button(submitButton);45 submit.click();
SauceConfigChangeServlet
Using AI Code Generation
1import com.paypal.selion.grid.servlets.SauceConfigChangeServlet;2import com.paypal.selion.grid.servlets.SauceConfigChangeServlet;3import org.openqa.grid.web.servlet.RegistryBasedServlet;4import org.openqa.grid.web.servlet.RegistryBasedServlet;5import org.openqa.selenium.remote.server.rest.ResultConfig;6import org.openqa.selenium.remote.server.rest.ResultConfig;7import org.openqa.selenium.remote.server.rest.Resu
SauceConfigChangeServlet
Using AI Code Generation
1package com.paypal.selion.grid.servlets;2import java.io.IOException;3import java.net.HttpURLConnection;4import java.net.MalformedURLException;5import java.net.URL;6import org.apache.commons.io.IOUtils;7import org.openqa.grid.common.exception.GridException;8import org.openqa.grid.internal.Registry;9import org.openqa.grid.internal.RemoteProxy;10import org.openqa.grid.internal.TestSession;11import org.openqa.grid.web.servlet.RegistryBasedServlet;12import com.google.gson.JsonObject;13import com.google.gson.JsonParser;14public class SauceConfigChangeServlet extends RegistryBasedServlet {15 public SauceConfigChangeServlet() {16 this(null);17 }18 public SauceConfigChangeServlet(Registry registry) {19 super(registry);20 }21 protected void doPost(javax.servlet.http.HttpServletRequest request,22 throws javax.servlet.ServletException, IOException {23 String pathInfo = request.getPathInfo();24 if (pathInfo == null) {25 response.setStatus(404);26 return;27 }28 String[] parts = pathInfo.split("/");29 if (parts.length != 2) {30 response.setStatus(404);31 return;32 }33 String proxyId = parts[1];34 RemoteProxy proxy = null;35 synchronized (getRegistry()) {36 for (RemoteProxy p : getRegistry().getAllProxies()) {37 if (p.getId().equals(proxyId)) {38 proxy = p;39 break;40 }41 }42 }43 if (proxy == null) {44 response.setStatus(404);45 return;46 }47 String body = IOUtils.toString(request.getReader());48 JsonObject obj = new JsonParser().parse(body).getAsJsonObject();49 String sessionId = obj.get("sessionId").getAsString();50 String sauceConfig = obj.get("sauceConfig").getAsString();51 TestSession session = null;52 synchronized (getRegistry()) {53 for (TestSession s : proxy.getTestSessions()) {54 if (s.getExternalKey().getKey().equals(sessionId)) {55 session = s;56 break;57 }58 }59 }60 if (session == null) {61 response.setStatus(404);62 return;63 }64 try {65 + "/jobs/" + sessionId + "/assets");66 HttpURLConnection conn = (HttpURLConnection) url.openConnection
SauceConfigChangeServlet
Using AI Code Generation
1import com.paypal.selion.grid.servlets.SauceConfigChangeServlet;2import java.io.BufferedReader;3import java.io.DataOutputStream;4import java.io.InputStreamReader;5import java.net.HttpURLConnection;6import java.net.URL;7import java.util.HashMap;8import java.util.Map;9import org.json.simple.JSONObject;10import org.json.simple.parser.JSONParser;11import org.json.simple.parser.ParseException;12public class 3 {13public static void main(String[] args) throws Exception {14SauceConfigChangeServlet sauceConfigChangeServlet = new SauceConfigChangeServlet();15URL obj = new URL(url);16HttpURLConnection con = (HttpURLConnection) obj.openConnection();17con.setRequestMethod("POST");18Map<String, String> params = new HashMap<String, String>();19params.put("sauceUserName", "Your Sauce Username");20params.put("sauceAccessKey", "Your Sauce Access Key");21params.put("sauceMaxSession", "10");22params.put("sauceBrowserTimeout", "60");23params.put("sauceCommandTimeout", "180");24params.put("sauceIdleTimeout", "120");25params.put("sauceMaxDuration", "3600");26params.put("sauceExtendedDebugging", "true");27params.put("sauceVideoUploadOnPass", "false");28params.put("sauceTunnelIdentifier", "Your Sauce Tunnel Identifier");29params.put("sauceParentTunnel", "Your Sauce Parent Tunnel");30params.put("sauceBuild", "Your Sauce Build");31params.put("sauceTags", "Your Sauce Tags");32params.put("sauceName", "Your Sauce Name");33con.setDoOutput(true);34DataOutputStream out = new DataOutputStream(con.getOutputStream());35out.writeBytes(ParameterStringBuilder.getParamsString(params));36out.flush();37out.close();38int responseCode = con.getResponseCode();39System.out.println("
SauceConfigChangeServlet
Using AI Code Generation
1package com.paypal.selion.grid;2import java.io.IOException;3import org.openqa.selenium.remote.DesiredCapabilities;4import org.openqa.selenium.remote.RemoteWebDriver;5import org.openqa.selenium.support.ui.WebDriverWait;6import org.testng.annotations.AfterTest;7import org.testng.annotations.BeforeTest;8import org.testng.annotations.Test;9import com.paypal.selion.annotations.WebTest;10import com.paypal.selion.platform.grid.Grid;11import com.paypal.selion.platform.grid.Grid.driver;12import com.paypal.selion.platform.grid.browsercapabilities.DefaultCapabilitiesBuilder;13import com.paypal.selion.platform.grid.browsercapabilities.SauceLabsCapabilitiesBuilder;14public class SauceConfigChangeServletTest {15 private WebDriverWait wait;16 private RemoteWebDriver driver;17 public void setup() throws IOException {18 DesiredCapabilities capabilities = new DefaultCapabilitiesBuilder().getCapabilities();19 capabilities = new SauceLabsCapabilitiesBuilder(capabilities).getCapabilities();20 wait = new WebDriverWait(driver, 30);21 }22 public void testSauceConfigChangeServlet() throws IOException {23 System.out.println(driver.getTitle());24 }25 public void tearDown() {26 driver.quit();27 }28}29package com.paypal.selion.grid;30import java31import java.io.InputStreamReader;32import java.net.HttpURLConnection;33import java.net.URL;34import java.util.HashMap;35import java.util.Map;36import org.json.simple.JSONObject;37import org.json.simple.parser.JSONParser;38import org.json.simple.parser.ParseException;39public class 3 {40public static void main(String[] args) throws Exception {41SauceConfigChangeServlet sauceConfigChangeServlet = new SauceConfigChangeServlet();42URL obj = new URL(url);43HttpURLConnection con = (HttpURLConnection) obj.openConnection();44con.setRequestMethod("POST");45Map<String, String> params = new HashMap<String, String>();46params.put("sauceUserName", "Your Sauce Username");47params.put("sauceAccessKey", "Your Sauce Access Key");48params.put("sauceMaxSession", "10");49params.put("sauceBrowserTimeout", "60");50params.put("sauceCommandTimeout", "180");51params.put("sauceIdleTimeout", "120");52params.put("sauceMaxDuration", "3600");53params.put("sauceExtendedDebugging", "true");54params.put("sauceVideoUploadOnPass", "false");55params.put("sauceTunnelIdentifier", "Your Sauce Tunnel Identifier");56params.put("sauceParentTunnel", "Your Sauce Parent Tunnel");57params.put("sauceBuild", "Your Sauce Build");58params.put("sauceTags", "Your Sauce Tags");59params.put("sauceName", "Your Sauce Name");60con.setDoOutput(true);61DataOutputStream out = new DataOutputStream(con.getOutputStream());62out.writeBytes(ParameterStringBuilder.getParamsString(params));63out.flush();64out.close();65int responseCode = con.getResponseCode();66System.out.println("
SauceConfigChangeServlet
Using AI Code Generation
1package com.paypal.selion.grid;2import java.io.IOException;3import org.openqa.selenium.remote.DesiredCapabilities;4import org.openqa.selenium.remote.RemoteWebDriver;5import org.openqa.selenium.support.ui.WebDriverWait;6import org.testng.annotations.AfterTest;7import org.testng.annotations.BeforeTest;8import org.testng.annotations.Test;9import com.paypal.selion.annotations.WebTest;10import com.paypal.selion.platform.grid.Grid;11import com.paypal.selion.platform.grid.Grid.driver;12import com.paypal.selion.platform.grid.browsercapabilities.DefaultCapabilitiesBuilder;13import com.paypal.selion.platform.grid.browsercapabilities.SauceLabsCapabilitiesBuilder;14public class SauceConfigChangeServletTest {15 private WebDriverWait wait;16 private RemoteWebDriver driver;17 public void setup() throws IOException {18 DesiredCapabilities capabilities = new DefaultCapabilitiesBuilder().getCapabilities();19 capabilities = new SauceLabsCapabilitiesBuilder(capabilities).getCapabilities();20 wait = new WebDriverWait(driver, 30);21 }22 public void testSauceConfigChangeServlet() throws IOException {23 System.out.println(driver.getTitle());24 }25 public void tearDown() {26 driver.quit();27 }28}29package com.paypal.selion.grid;30import java
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!