All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sitoolkit.tester.selenium.OpenOperation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 Monocrea Inc.
 *
 * 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 org.sitoolkit.tester.selenium;

import java.io.File;
import java.net.MalformedURLException;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.sitoolkit.tester.TestContext;
import org.sitoolkit.tester.TestException;
import org.sitoolkit.tester.TestScript;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author yuichi.kuwahara
 */
public class OpenOperation extends SeleniumOperation {

	@Autowired
	protected TestContext current;

	/**
	 * テストスクリプトで指定されたURLを開きます。
	 * URLはロケーターの値とシステムプロパティbaseUrlを連結した文字列です。
	 * ロケーターの値がhttp(s)で始まる場合はbaseUrlは無視されます。
	 * ロケーターの値、baseUrl何れもhttp(s)で始まらない場合は、
	 * ファイルプロトコルのURLとして解釈します。
	 *
	 *
	 * @see WebDriver#get(String)
	 */
	public void execute(TestScript testScript) {
		String url = buildUrl(System.getProperty("baseUrl"), testScript.getLocator().getValue());
		info("オープン", null);
		seleniumDriver.get(url);
	}

	/**
	 * オープン先となるURLを構築します。
	 *
	 * @param baseUrl 基準となるURL
	 * @param path 基準となるURLからの相対パス
	 * @return オープン先となるURLの文字列
	 */
	protected String buildUrl(String baseUrl, String path) {
		if (path.startsWith("http:") || path.startsWith("https:")) {
			return path;
		}
		if (StringUtils.isEmpty(baseUrl)) {
			return file2url(path);
		} else {
			if (baseUrl.startsWith("http:") || baseUrl.startsWith("https:")) {
				return concatPath(baseUrl, path);
			} else {
				return concatPath(file2url(baseUrl), path);
			}
		}
	}

	private String file2url(String url) {
		try {
			return new File(url).toURI().toURL().toString();
		} catch (MalformedURLException e) {
			throw new TestException(e);
		}
	}

	private String concatPath(String a, String b) {
		return a.endsWith("/")
				? a + b
				: a + "/" + b;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy