12/08/2018, 17:10

Selenium Basic: Textbox, dropbox,checkbox and radio button

package learnSelenium; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.suppor ...

package learnSelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class testEdit {
	private WebDriver driver = null;

	@BeforeMethod
	public void init() {
		System.setProperty("webdriver.chrome.driver", "E:Setupchromedriver_win32chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("https://selenium-training.herokuapp.com/");
	}

	@AfterMethod
	public void end() {
		 driver.quit();
	}

	public void testLogin() {
		driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MICROSECONDS);

		WebElement btnSignin = driver.findElement(By.cssSelector("ul li a[href='/login']"));
		btnSignin.click();

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		WebElement txtEmail = driver.findElement(By.cssSelector("form input[name='session[email]']"));
		txtEmail.sendKeys("nguyennhung@mail.com");
		WebElement txtPassword = driver.findElement(By.cssSelector("form input[name = 'session[password]']"));
		txtPassword.sendKeys("12345678");

		WebElement btncheck = driver
				.findElement(By.cssSelector("form input[type='checkbox'][name='session[remember_me]']"));
		btncheck.click();

		WebElement btnRegister1 = driver.findElement(By.name("commit"));
		btnRegister1.submit();

		WebDriverWait wait = new WebDriverWait(driver, 15);
		wait.until(ExpectedConditions.urlContains("https://selenium-training.herokuapp.com/users/306"));
	}

	@Test
	public void testEdit() {
		testLogin();
		WebElement btnSignin1 = driver.findElement(By.cssSelector("ul li[class='dropdown'] a"));
		btnSignin1.click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		WebElement btnSignin = driver.findElement(By.cssSelector("ul li a[href='/users/306/edit']"));
		btnSignin.click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MICROSECONDS);
		
		WebElement fullName = driver.findElement(By.cssSelector("form input[name='user[name]']"));
		fullName.clear();
		fullName.sendKeys("NhungNTH1");
		String inputtedName = fullName.getAttribute("value");
		Assert.assertEquals("NhungNTH1", inputtedName);
		
		WebElement birthday = driver.findElement(By.cssSelector("form input[name='user[birth_day]']"));
		birthday.sendKeys("02/28/2017");
		String inputtedBD = birthday.getAttribute("value");
		//System.out.println(inputtedBD);
		Assert.assertEquals("2017-02-28", inputtedBD);
		
		Select select = new Select(driver.findElement(By.cssSelector("form select[name='user[language]']")));
		select.selectByValue("2");
		String selectedValue = select.getFirstSelectedOption().getAttribute("value");
		Assert.assertEquals("2", selectedValue);
		
		//System.out.println("option:" + select.getFirstSelectedOption().getText());
		//System.out.println("value: " + select.getFirstSelectedOption().getAttribute("value"));
		
		WebElement phone = driver.findElement(By.cssSelector("form input[name='user[phone]']"));
		phone.sendKeys("0123456789");

		WebElement gender = driver.findElement(By.cssSelector("form input[name='user[gender]'][value='2']"));
		gender.click();
		String inputtedGender = gender.getAttribute("value");
		//System.out.println(inputtedGender);
		Assert.assertEquals("2", inputtedGender);
		//System.out.println("Radio: " + gender.isSelected());
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
0