<commit_before>/* Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package geb

import geb.driver.*
import geb.js.*
import geb.conf.*
import geb.internal.WaitingSupport
import geb.internal.RemoteDriverOperations
import geb.error.PageChangeListenerAlreadyRegisteredException
import geb.error.RequiredPageContentNotPresent
import geb.error.UnexpectedPageException
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebDriverException

class Browser {

	Page page
	final WebDriver driver
	final Configuration config
	String baseUrl
	
	@Delegate final WaitingSupport _waitingSupport = new WaitingSupport()
	@Delegate final AlertAndConfirmSupport  _alertAndConfirmSupport = new AlertAndConfirmSupport({ this.getJs() }) 
	
	private final pageChangeListeners = new LinkedHashSet()
	private _js

	/**
	 * If the driver is remote, this object allows access to it's capabilities. This indirection is needed
	 * to avoid a hard dependency on the remote driver classes which aren't part of core.
	 * 
	 * @see RemoteDriverOperations
	 */
	@Lazy WebDriver augmentedDriver = new RemoteDriverOperations(this.class.classLoader).getAugmentedDriver(driver)
	
	Browser(Class pageClass, Map params = null) {
		this(null, null, pageClass, params)
	}
	
	Browser(String baseUrl, Class pageClass = null, Map params = null) {
		this(null, baseUrl, pageClass)
	}
	
	Browser(WebDriver driver = null, String baseUrl = null, Class pageClass = null, Map params = null) {
		this.config = createConfiguration()
		this.driver = driver ?: defaultDriver
		this.baseUrl = baseUrl
		
		params = params == null ? [:] : params
		
		if (pageClass) {
			to(pageClass, *:params)
		} else {
			page Page
		}
	}
	
	protected Configuration createConfiguration() {
		def configurationLocation = getConfigurationLocation()
		if (configurationLocation) {
			createConfigurationLoader().load(configurationLocation)
		} else {
			new Configuration()
		}
	}
	
	protected URL getConfigurationLocation() {
		getClass().classLoader.getResource("geb-conf.groovy")
	}
	
	protected ConfigurationLoader createConfigurationLoader() {
		new ConfigurationLoader(getConfigurationEnvironment(), System.properties)
	}
	
	protected String getConfigurationEnvironment() {
		System.getProperty("geb.env")
	}
	
	protected WebDriver getDefaultDriver() {
		config.driverInstance
	}
		
	void registerPageChangeListener(PageChangeListener listener) {
		if (pageChangeListeners.add(listener)) {
			listener.pageWillChange(this, null, page)<commit_msg>Load the geb config using the context classloader instead of browser class's classloader.<commit_after>/* Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package geb

import geb.driver.*
import geb.js.*
import geb.conf.*
import geb.internal.WaitingSupport
import geb.internal.RemoteDriverOperations
import geb.error.PageChangeListenerAlreadyRegisteredException
import geb.error.RequiredPageContentNotPresent
import geb.error.UnexpectedPageException
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebDriverException

class Browser {

	Page page
	final WebDriver driver
	final Configuration config
	String baseUrl
	
	@Delegate final WaitingSupport _waitingSupport = new WaitingSupport()
	@Delegate final AlertAndConfirmSupport  _alertAndConfirmSupport = new AlertAndConfirmSupport({ this.getJs() }) 
	
	private final pageChangeListeners = new LinkedHashSet()
	private _js

	/**
	 * If the driver is remote, this object allows access to it's capabilities. This indirection is needed
	 * to avoid a hard dependency on the remote driver classes which aren't part of core.
	 * 
	 * @see RemoteDriverOperations
	 */
	@Lazy WebDriver augmentedDriver = new RemoteDriverOperations(this.class.classLoader).getAugmentedDriver(driver)
	
	Browser(Class pageClass, Map params = null) {
		this(null, null, pageClass, params)
	}
	
	Browser(String baseUrl, Class pageClass = null, Map params = null) {
		this(null, baseUrl, pageClass)
	}
	
	Browser(WebDriver driver = null, String baseUrl = null, Class pageClass = null, Map params = null) {
		this.config = createConfiguration()
		this.driver = driver ?: defaultDriver
		this.baseUrl = baseUrl
		
		params = params == null ? [:] : params
		
		if (pageClass) {
			to(pageClass, *:params)
		} else {
			page Page
		}
	}
	
	protected Configuration createConfiguration() {
		def configurationLocation = getConfigurationLocation()
		if (configurationLocation) {
			createConfigurationLoader().load(configurationLocation)
		} else {
			new Configuration()
		}
	}
	
	protected URL getConfigurationLocation() {
		Thread.currentThread().contextClassLoader.getResource("geb-conf.groovy")
	}
	
	protected ConfigurationLoader createConfigurationLoader() {
		new ConfigurationLoader(getConfigurationEnvironment(), System.properties)
	}
	
	protected String getConfigurationEnvironment() {
		System.getProperty("geb.env")
	}
	
	protected WebDriver getDefaultDriver() {
		config.driverInstance
	}
		
	void registerPageChangeListener(PageChangeListener listener) {
		if (pageChangeListeners.add(listener)) {
			listener.pageWillChange(this, null, page)
{'fraction_non_alphanumeric': 0.07985158896596226, 'fraction_numerical': 0.0025810614615260526, 'mean_word_length': 4.724838411819022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2396673', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1595, 'n_words': 543}