How to use valueOf method of org.openqa.selenium.Enum PageLoadStrategy class

Best Selenium code snippet using org.openqa.selenium.Enum PageLoadStrategy.valueOf

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.assessment.common;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.util.List;
6import java.util.concurrent.TimeUnit;
7
8import org.openqa.selenium.By;
9import org.openqa.selenium.PageLoadStrategy;
10import org.openqa.selenium.WebDriver;
11import org.openqa.selenium.WebElement;
12import org.openqa.selenium.chrome.ChromeDriver;
13import org.openqa.selenium.chrome.ChromeDriverService;
14import org.openqa.selenium.chrome.ChromeOptions;
15import org.openqa.selenium.firefox.FirefoxDriver;
16import org.openqa.selenium.firefox.FirefoxOptions;
17import org.openqa.selenium.interactions.Actions;
18import org.openqa.selenium.remote.CapabilityType;
19import org.testng.Assert;
20
21public class BaseSelenium {
22	protected WebDriver driver;
23
24	protected enum LOCATOR_TYPE {
25		ID, NAME, LINK, XPATH, JSSCRIPT, PARTIALLINK, CSS
26	};
27	public static void initializeVariable() throws Exception {
28		String workingDir = System.getProperty("user.dir");
29		System.out.println(workingDir);
30
31		try {
32			if (!Constants.is_initialized_variable) {
33				Constants.prop.load(new FileInputStream(new File(workingDir + "/Automation.properties")));
34				Constants.base_url = Constants.prop.getProperty("BaseURL");
35				Constants.browser = Constants.prop.getProperty("Browser");
36				Constants.firefox_driver_path = workingDir + "/" + Constants.prop.getProperty("FirefoxDriverPath");
37				Constants.chrome_driver_path = workingDir + "/" + Constants.prop.getProperty("ChromeDriverPath");
38				Constants.ie_driver_path = Constants.prop.getProperty("IEDriverPath");
39				Constants.explicit_wait_time = Integer.parseInt(Constants.prop.getProperty("Explicit_wait_time"));
40				Constants.implicit_wait_time = Integer.parseInt(Constants.prop.getProperty("Implicit_wait_time"));
41				Constants.page_load_time = Integer.parseInt(Constants.prop.getProperty("Max_page_load_time"));
42				
43				System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
44				System.setProperty("webdriver.gecko.driver", Constants.firefox_driver_path);
45				System.setProperty("webdriver.chrome.driver", Constants.chrome_driver_path);
46				Constants.is_initialized_variable = true;
47			}
48		} catch (Exception e) {
49			System.out.println("Error during initialization of Variables, Hence Quittting");
50			e.printStackTrace();
51			System.exit(1);
52		}
53	}
54
55	public void OpenBrowser() throws Exception {
56		try {
57//			proxy = getProxyServer();
58//			Proxy seleniumProxy = getSeleniumProxy(proxy);
59			if (Constants.browser.toLowerCase().equals("firefox")) {
60				FirefoxOptions options = new FirefoxOptions();
61//				options.setCapability(CapabilityType.PROXY, seleniumProxy);
62				driver = new FirefoxDriver(options);
63			} else if (Constants.browser.toLowerCase().equals("chrome")) {
64				ChromeOptions options = new ChromeOptions();
65// 				options.setCapability(CapabilityType.PROXY, seleniumProxy);
66				options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
67				options.setCapability(ChromeOptions.CAPABILITY, true);
68				options.addArguments("--ignore-certificate-errors");
69				options.addArguments("--enable-automation");
70				options.addArguments("--window-size=1920x1080");
71				options.addArguments("--start-maximized");
72// 				options.addArguments("--headless");
73				options.addArguments("--disable-gpu");
74				options.addArguments("--disable-infobars");
75				// options.addArguments("--remote-debugging-port=9222");
76				options.addArguments("--no-sandbox");
77				options.addArguments("--disable-setuid-sandbox");
78				options.addArguments("--disable-dev-shm-usage");
79				options.addArguments("--dns-prefetch-disable");
80				options.addArguments("--disable-infobars");
81				options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
82
83				options.setCapability("silent", true);
84				driver = new ChromeDriver(options);
85				// driver=new ChromeDriver();
86
87			}
88
89			else {
90				throw new Exception("Browser not supported");
91			}
92
93			driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
94			driver.manage().window().maximize();
95			driver.get(Constants.base_url);
96			Thread.sleep(2000);
97		} catch (Exception e) {
98			System.out.println(e);
99			e.printStackTrace();
100			Assert.fail(e.getMessage());
101		}
102	}
103	
104	public void type(String Locator, String LocatorType, String Value)
105			throws Exception {
106		switch (LOCATOR_TYPE.valueOf(LocatorType)) {
107		case ID:
108			driver.findElement(By.id(Locator)).sendKeys(Value);
109			break;
110
111		case NAME:
112			driver.findElement(By.name(Locator)).sendKeys(Value);
113			break;
114
115		case XPATH:
116			driver.findElement(By.xpath(Locator)).sendKeys(Value);
117			break;
118
119		case CSS:
120			driver.findElement(By.cssSelector(Locator))
121					.sendKeys(Value);
122			break;
123
124		default:
125			throw new Exception(
126					"Invalid Locator Type/Locator Type may not be supported");
127		}
128	}
129	
130	
131	public void click(String Locator, String LocatorType) throws Exception {
132		//scrollIntoViewAndClick(Locator, LocatorType);
133		switch (LOCATOR_TYPE.valueOf(LocatorType)) {
134		case ID:
135			driver.findElement(By.id(Locator)).click();
136			break;
137
138		case NAME:
139			driver.findElement(By.name(Locator)).click();
140			break;
141
142		case XPATH:
143			driver.findElement(By.xpath(Locator)).click();
144			break;
145
146		case CSS:
147			driver.findElement(By.cssSelector(Locator)).click();
148			break;
149
150		case PARTIALLINK:
151			driver.findElement(By.partialLinkText(Locator)).click();
152			break;
153
154		case LINK:
155			driver.findElement(By.linkText(Locator)).click();
156			break;
157			
158
159		default:
160			throw new Exception(
161					"Invalid Locator Type/Locator Type may not be supported");
162		}
163}
164	
165	public List<WebElement> getWebElements (String Locator, String LocatorType) throws Exception {
166		//List<String> SearchOutPut=new ArrayList<String>();	
167		List<WebElement> WebObjects;
168			switch (LOCATOR_TYPE.valueOf(LocatorType)) {
169			case ID:
170				WebObjects=driver.findElements(By.id(Locator));
171				break;
172
173			case NAME:
174				WebObjects=driver.findElements(By.name(Locator));
175				break;
176
177			case XPATH:
178				WebObjects=driver.findElements(By.xpath(Locator));
179				break;
180
181			case CSS:
182				WebObjects=driver.findElements(By.cssSelector(Locator));
183				break;
184
185			case PARTIALLINK:
186				WebObjects=driver.findElements(By.partialLinkText(Locator));
187				break;
188
189			case LINK:
190				WebObjects=driver.findElements(By.linkText(Locator));
191				break;
192				
193			default:
194				throw new Exception(
195						"Invalid Locator Type/Locator Type may not be supported");
196			}
197			return WebObjects;
198		}
199	
200	public void mouseOver(WebElement element) {
201		Actions actions = new Actions(driver);
202		actions.moveToElement(element).perform();
203	}
204
205	public boolean checkElementExist(String Locator, String LocatorType)
206			throws Exception {
207		boolean ElementExists = false;
208		switch (LOCATOR_TYPE.valueOf(LocatorType)) {
209		case ID:
210			ElementExists = driver.findElements(By.id(Locator)).size() != 0;
211			break;
212
213		case NAME:
214			ElementExists = driver.findElements(By.name(Locator))
215					.size() != 0;
216			break;
217
218		case XPATH:
219			ElementExists = driver.findElements(By.xpath(Locator))
220					.size() != 0;
221			break;
222
223		case CSS:
224			ElementExists = driver.findElements(
225					By.cssSelector(Locator)).size() != 0;
226			break;
227		default:
228			throw new Exception(
229					"Invalid Locator Type/Locator Type may not be supported");
230		}
231	return ElementExists;	
232}
233	
234	public void switchToIFrame(String type,String frame,WebElement obj) throws Exception
235	{
236		if(type.equals("id")||type.equals("name"))
237		{
238			driver.switchTo().frame(frame);
239		}
240		else if(type.equals("webelement"))
241		{
242			driver.switchTo().frame(obj);
243		}
244		else
245		{
246			throw new Exception("Invalid frame type");
247		}
248	}
249	
250	public void switchToParentFrame() throws Exception
251	{
252	  driver.switchTo().parentFrame();	
253	}
254	
255	public WebElement getWebElement(String Locator, String LocatorType) throws Exception {
256		WebElement element;
257		switch (LOCATOR_TYPE.valueOf(LocatorType)) {
258		case ID:
259			return driver.findElement(By.id(Locator));
260
261		case NAME:
262			return driver.findElement(By.name(Locator));
263
264		case XPATH:
265			return driver.findElement(By.xpath(Locator));
266
267		case CSS:
268			return driver.findElement(By.cssSelector(Locator));
269			
270
271		case PARTIALLINK:
272			return driver.findElement(By.partialLinkText(Locator));
273
274		case LINK:
275			return driver.findElement(By.linkText(Locator));
276			
277		default:
278			throw new Exception(
279					"Invalid Locator Type/Locator Type may not be supported");
280		}
281	}
282}
283
Full Screen
copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium;
19
20public enum PageLoadStrategy {
21
22  NONE ("none"),
23  EAGER ("eager"),
24  NORMAL ("normal")
25  ;
26
27  private String text;
28
29  private PageLoadStrategy(String text) {
30    this.text = text;
31  }
32
33  @Override
34  public String toString() {
35    return String.valueOf(text);
36  }
37
38  public static PageLoadStrategy fromString(String text) {
39    if (text != null) {
40      for (PageLoadStrategy b : PageLoadStrategy.values()) {
41        if (text.equalsIgnoreCase(b.text)) {
42          return b;
43        }
44      }
45    }
46    return null;
47  }
48}
49
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Enum-PageLoadStrategy

Trigger valueOf code on LambdaTest Cloud Grid

Execute automation tests with valueOf on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie