1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Selenium WebDriver Java Simple Program - Login

Last updated at Posted at 2022-03-28

Outline

Selenium WebDriverで実際にE2E テストを作る。
初心者にとって、E2Eテスト自動化がどのようなものかを知るため、簡単なログイン機能のテストを実際にコードにしてみた。
尚、開発環境構築は https://qiita.com/emurin/items/b4c04e5030835b98159a を参考のこと。

Test Scenario

  1. Browserを開き、my.rakutenに遷移する
  2. ログインをクリックする
  3. Rakuten IDとpasswordを入力する
  4. ログインをクリックする
  5. ログインの可否を、名前が正しいかで判断する
  6. Browserを閉じる

Repositories

プログラミングする前に、操作対象となるHTML要素(repositories)を抽出する。
たとえば、my.rakuten.co.jpの「ログイン」のボタンを特製するとき、chrome developer toolで下図のように、html要素を確認し、ろのログインボタンを一意に特定する要素を取り出す。

今回のプログラムでは、Xpathを用いて特定した。
 //a[@class='btn loginbtn']
こちらが、ログインボタンの要素である。

image.png

このようにして、テストシナリオに必要なrepositoriesをすべて抽出する。

Program

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import net.bytebuddy.asm.Advice.This;
import static org.junit.Assert.assertEquals;

public class SampleScript {
	
	
	protected static String id;
	protected static String pwd;
	protected static String username;
	

	public static void main (String[] args) {
		WebDriver driver = new ChromeDriver();

		id = "Rakuten ID";
		pwd = "Rakuten ID password";
		username = "User ID name";
		
		// 1. open browser
		driver.get("https://my.rakuten.co.jp/");

		System.out.println("[INFO] Click login");
		Actions builder = new Actions(driver);

		// 2. go to login page		
		WebElement buttonMypagelogin = driver.findElement(By.xpath("//a[@class='btn loginbtn']"));
		builder.click(buttonMypagelogin).perform();

				
		System.out.println("[INFO] Input ID & Password");
		// 3. input ID & password
		WebElement loginInner_u = driver.findElement(By.xpath("//input[@id='loginInner_u']"));
		builder.sendKeys(loginInner_u,id).perform();
		
		WebElement loginInner_p = driver.findElement(By.xpath("//input[@id='loginInner_p']"));
		builder.sendKeys(loginInner_p,pwd).perform();
		
		System.out.println("[INFO] Click LoginButton");
		// 4. click login
		WebElement loginButton = driver.findElement(By.xpath("//input[@class='loginButton']"));
		builder.click(loginButton).perform();
		
		// 5. validate login status
		WebElement mystatus_name = driver.findElement(By.xpath("//em[@id='mystatus_name']"));
		String mystatus_name_innertext = mystatus_name.getText();

		System.out.println("[INFO] Validate mystatus_name_innertext = " + mystatus_name_innertext);
		assertEquals(username, mystatus_name_innertext);
		
		// 6. close browser
		driver.close();
				
	}
}

プログラムにあたっての特出するポイント

  • https://www.selenium.dev/ja/documentation/webdriver/ のseleniumのIF仕様を参考
  • 基本的にRepositoriesを取得し、アクションするの繰り返し
  • assertionはJUnitの関数(assertEquals)を使用(対象のサンプルをJUnitを使えるようにすること)
  • ID,Password,Usernameは自分で用意すること

Result

Videotogif (2).gif

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?