<commit_before>/******************************************************
 * Navigation timing
 * 
 *
 * Copyright (C) 2013 by <NAME> (http://peterhedenskog.com)
 *
 ******************************************************
 *
 * 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 com.soulgalore.web.performance.navigation;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import com.google.inject.Inject;
import com.soulgalore.web.performance.navigation.run.NavigationTimingConfiguration;

/**
 * Selenium backend for getting the Navigation Timing data.
 * 
 */
public class NavigationTimingSeleniumCreator implements NavigationTimingCreator {

	private final WebDriver driver;

	private static final String TIMING = "performance.timing.";

	private static final String SELENIUM = "return window.";

	@Inject
	public NavigationTimingSeleniumCreator(WebDriver driver) {
		this.driver = driver;
	}

	public NavigationTiming get(String url, String name) {
		try {
			driver.get(url);

			// hack to get browser & version
			Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
			String browserName = caps.getBrowserName();
			String browserVersion = caps.getVersion();

			JavascriptExecutor js = (JavascriptExecutor) driver;

			verifyNavigationTiminSupport(js, browserName, browserVersion);

			Map<String, Long> timings = new HashMap<String, Long>();

			for (String timing : NavigationTimingData.DEFAULT_NAV_TIMINGS) {
				timings.put(timing,
						(Long) js.executeScript(SELENIUM + TIMING + timing));
			}

			// TODO check this test
			if (browserName.toLowerCase().contains("explorer")) {
				// in milliseconds
				timings.put(
						"msFirstPaint",
						(Long) js.executeScript(SELENIUM + TIMING
								+ "msFirstPaint"));
			}

			
			else if (browserName.toLowerCase().contains("chrome")) {
				// TODO cleanup names
				timings.put(
						"wasFetchedViaSpdy",
						(Boolean) js
								.executeScript("return window.chrome.loadTimes().wasFetchedViaSpdy") ? new Long(
								1) : new Long(0));
	
		
				// TODO: in seconds.ms, convert it to ms!!
				Double time = (Double) js.executeScript("return window.chrome.loadTimes().firstPaintTime");
				timings.put("firstPaintTime", time.longValue());
			}

			return new NavigationTiming(new TestMetaData(url, browserName,
					browserVersion, new Date(), name),
					new NavigationTimingData(timings));
		} finally {
			if (driver != null) {
				driver.quit();

			}
		}
	}

	private void verifyNavigationTiminSupport(JavascriptExecutor js,
			String browserName, String browserVersion) {<commit_msg>converting chrome firtspaint to millis<commit_after>/******************************************************
 * Navigation timing
 * 
 *
 * Copyright (C) 2013 by <NAME> (http://peterhedenskog.com)
 *
 ******************************************************
 *
 * 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 com.soulgalore.web.performance.navigation;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import com.google.inject.Inject;
import com.soulgalore.web.performance.navigation.run.NavigationTimingConfiguration;

/**
 * Selenium backend for getting the Navigation Timing data.
 * 
 */
public class NavigationTimingSeleniumCreator implements NavigationTimingCreator {

	private final WebDriver driver;

	private static final String TIMING = "performance.timing.";

	private static final String SELENIUM = "return window.";

	@Inject
	public NavigationTimingSeleniumCreator(WebDriver driver) {
		this.driver = driver;
	}

	public NavigationTiming get(String url, String name) {
		try {
			driver.get(url);

			// hack to get browser & version
			Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
			String browserName = caps.getBrowserName();
			String browserVersion = caps.getVersion();

			JavascriptExecutor js = (JavascriptExecutor) driver;

			verifyNavigationTiminSupport(js, browserName, browserVersion);

			Map<String, Long> timings = new HashMap<String, Long>();

			for (String timing : NavigationTimingData.DEFAULT_NAV_TIMINGS) {
				timings.put(timing,
						(Long) js.executeScript(SELENIUM + TIMING + timing));
			}

			// TODO check this test
			if (browserName.toLowerCase().contains("explorer")) {
				// in milliseconds
				timings.put(
						"msFirstPaint",
						(Long) js.executeScript(SELENIUM + TIMING
								+ "msFirstPaint"));
			}

			
			else if (browserName.toLowerCase().contains("chrome")) {
				// TODO cleanup names
				timings.put(
						"wasFetchedViaSpdy",
						(Boolean) js
								.executeScript("return window.chrome.loadTimes().wasFetchedViaSpdy") ? new Long(
								1) : new Long(0));
	
		
				// Chrome timing is in s.ms, convert it to ms!!
				Double time = (Double) js.executeScript("return window.chrome.loadTimes().firstPaintTime");
				timings.put("firstPaintTime", ((long)(time*1000)));
			}

			return new NavigationTiming(new TestMetaData(url, browserName,
					browserVersion, new Date(), name),
					new NavigationTimingData(timings));
		} finally {
			if (driver != null) {
				driver.quit();

			}
		}
	}

	private void verifyNavigationTiminSupport(JavascriptExecutor js,
			String browserName, String browserVersion) {
{'fraction_non_alphanumeric': 0.14719452299449323, 'fraction_numerical': 0.0035719601131120704, 'mean_word_length': 4.153374233128834, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2897971', 'n_tokens_mistral': 2206, 'n_tokens_neox': 1852, 'n_words': 520}