웹사이트 검색

셀레늄 웹드라이버


웹 애플리케이션을 테스트하려면 브라우저가 필요합니다. Selenium은 브라우저를 자동화하고 다양한 브라우저에서 웹 애플리케이션 테스트를 자동화하는 데 도움이 됩니다. Selenium API는 다양한 유형의 브라우저 및 HTML 요소와 함께 작동하도록 많은 클래스와 인터페이스를 제공했습니다.

Selenium WebDriver 인터페이스는 무엇입니까?

Selenium WebDriver는 일련의 메서드를 정의하는 인터페이스입니다. 그러나 구현은 브라우저 특정 클래스에서 제공됩니다. 일부 구현 클래스는 AndroidDriver, ChromeDriver, FirefoxDriver, InternetExplorerDriver, IPhoneDriver입니다. , SafariDriver 등 WebDriver의 주요 기능은 브라우저를 제어하는 것입니다. HTML 페이지 요소를 선택하고 클릭, 양식 필드 채우기 등과 같은 작업을 수행하는 데에도 도움이 됩니다.

Firefox 브라우저에서 테스트 사례를 실행하려면 FirefoxDriver 클래스를 사용해야 합니다. 마찬가지로 Chrome 브라우저에서 테스트 사례를 실행하려면 ChromeDriver 클래스를 사용해야 합니다.

Selenium WebDriver 메서드

SearchContext는 findElement() 및 findElements()의 두 가지 메소드가 있는 Selenium API의 최상위 인터페이스입니다. Selenium WebDriver 인터페이스에는 get(String url), quit(), close(), getWindowHandle(), getWindowHandles(), getTitle() 등과 같은 많은 추상 메서드가 있습니다. WebDriver에는 Window, <Navigation, Timeouts 등. 이러한 중첩 인터페이스는 back(), forward() 등과 같은 작업을 수행하는 데 사용됩니다.

Method Description
get(String url) This method will launch a new browser and opens the given URL in the browser instance.
getWindowHandle() It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser.
getWindowHandles() It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver.
close() This command is used to close the current browser window which is currently in focus.
quit() This method will closes all the browsers windows which are currently opened and terminates the WebDriver session.
getTitle() This method is used to retrieve the title of the webpage the user currently working on.

WebDriver를 구현하는 클래스 목록

WebDriver 인터페이스의 주요 구현 클래스는 ChromeDriver, EdgeDriver, FirefoxDriver, InternetExplorerDriver 등입니다. 각 드라이버 클래스는 브라우저에 해당합니다. 우리는 단순히 드라이버 클래스의 개체를 만들고 작업합니다.

Class Description
ChromeDriver It helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriver It helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriver It helps you to execute Selenium Scripts on InternetExplorer browser.

WebElement의 명령 목록

Selenium WebElement는 HTML 요소를 나타냅니다. findElement() 메서드를 사용하여 WebElement의 인스턴스를 가져온 다음 클릭, 제출 등과 같은 특정 작업을 수행할 수 있습니다. 일반적으로 사용되는 WebElement 메서드 중 일부는 다음과 같습니다.

Command Description Syntax
findElement() This method finds the first element within the current web page by using given locator. WebElement element = driverObject.findElement(By.locator(“value”));
sendKeys() This method enters a value in to an Edit Box or Text box. driver.findElement(By.elementLocator(“value”)).sendkeys(“value”);
clear() It clears the Value from an Edit box or Text Box. driverObject.findElement(By.locatorname(“value”)).clear();
click() It clicks an Element (Button, Link, Checkbox) etc. driverObject.findElement(By.ElementLocator(“LocatorValue”)).click();

Selenium WebDriver 예제 - 웹사이트 제목 인쇄

Selenium WebDriver를 사용하여 Firefox 브라우저를 호출하고 웹 사이트의 제목을 인쇄하는 간단한 예를 살펴보겠습니다.

package com.journaldev.selenium.firefox;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriverExample {

	public static void main(String[] args) {
		//specify the location of GeckoDriver for Firefox browser automation
		System.setProperty("webdriver.gecko.driver", "geckodriver");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://journaldev.com");
		String PageTitle = driver.getTitle();
		System.out.println("Page Title is:" + PageTitle);
		driver.close();
	}
}

산출:

1551941763563	mozrunner::runner	INFO	Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297	addons.webextension.screenshots@mozilla.org	WARN	Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794	Marionette	INFO	Listening on port 61417
1551941765818	Marionette	WARN	TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652	Marionette	INFO	Stopped listening on port 61417

GitHub 리포지토리에서 더 많은 Selenium 예제를 확인할 수 있습니다.

참조: WebDriver GitHub 코드