Menu

All Posts by testautomation

About the Author

Aug 27

HardAssertionExample

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class HardAssertionExample {
	WebDriver driver;

	@Test
	public void testHardAssertion() {

		driver.get("https://www.google.com");
		String titleOfGoogleHomePage = driver.getTitle();
        String expectedTitleOfGoogleHomePate = "Google";
        String searchTerm = null;
        //searchTerm = "TestAutomation.co";
        Assert.assertEquals(titleOfGoogleHomePage, expectedTitleOfGoogleHomePate);	
        
		WebElement searchBox = driver.findElement(By.xpath("//input[@title='Search' and @type='text']"));
		Assert.assertNotNull(searchTerm);
		searchBox.sendKeys(searchTerm);
		WebElement submitSearchBtn = driver.findElement(By.name("btnK"));
		submitSearchBtn.submit();

	}

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();

	}

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

}
Aug 27

MyTestNGClass

By testautomation | Selenium Code

package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MyTestNGClass {
	WebDriver driver;

	@BeforeMethod
	public void prepareTest() {
		System.setProperty("webdriver.ie.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();

	}

	@Test
	public void runTest() {
		driver.get("https://www.google.com");
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		WebElement searchBox = driver.findElement(By.xpath("//input[@title='Search' and @type='text']"));
		searchBox.sendKeys("TestAutomation.co");
		WebElement submitSearchBtn = driver.findElement(By.name("btnK"));
		submitSearchBtn.submit();

	}

	@AfterMethod
	public void cleanTest() {
		driver.quit();

	}

}
Aug 27

IEDriverTest

By testautomation | Selenium Code

package selenium;

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.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class IEDriverTest {

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.ie.driver", "C:\\IEDriver\\IEDriverServer_Win32_3.14.0\\IEDriverServer.exe");
		WebDriver driver = new InternetExplorerDriver();
		driver.get("https://www.google.com");
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		// System.out.println("Its page source - " + driver.getPageSource());
		WebElement searchBox = driver.findElement(By.name("q"));
		// input[@title='Search' and @type='text']
		// WebElement searchBox = driver.findElement(By.xpath("//input[@title='Search'
		// and @type='text']"));
		searchBox.sendKeys("TestAutomation.co");
		WebElement submitSearchBtn = driver.findElement(By.name("btnK"));
		submitSearchBtn.submit();
//		
		//driver.quit();

	}

}
Aug 27

ExplicitTestNG

By testautomation | Selenium Code

package selenium;


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

public class ExplicitTestNG {

	WebDriver driver = null;

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();

	}

	@Test
	public void testExplicit() {
		driver.get("https://testautomation.co/webelements/");
		WebDriverWait wait = new WebDriverWait(driver,10);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("explibutton")));
		driver.findElement(By.id("explibutton")).click();

		

	}

	@AfterMethod
	public void afterMethod() {
		// driver.quit();

	}

}
Aug 27

ImplicitTestNG

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class ImplicitTestNG {
	WebDriver driver = null;

	@Test
	public void testImplicit() {
		driver.get("https://testautomation.co/webelements/");
		driver.findElement(By.id("link2courses1")).click();
		

	}

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
	}

	@AfterMethod
	public void afterMethod() {
		// driver.quit();

	}

}
Aug 27

ElementById

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class ElementById {

	WebDriver driver = null;

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
	}

	//
	@Test
	public void testById() {
		driver.get("https://testautomation.co/webelements/");
		WebElement name = driver.findElement(By.id("legalname"));
		name.sendKeys("John");
		name.clear();
		name.sendKeys("John Doe");
		System.out.println(name.isDisplayed());
		System.out.println(name.isEnabled());
		System.out.println(name.isSelected());
		
	}

	@AfterMethod
	public void afterMethod() {
		// driver.quit();

	}

}
Aug 27

ElementByXpath

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class ElementByXpath {
	WebDriver driver = null;

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
	}

	@Test
	public void testByXpath() {
		driver.get("https://testautomation.co/webelements/");
		//driver.findElement(By.xpath("//*[@id=\"formsubmit\"]/table/tbody/tr[6]/td[1]/input")).click();;
		driver.findElement(By.xpath("//input[@type=\"submit\"]")).click();
		
	}

	@AfterMethod
	public void afterMethod() {
		// driver.quit();

	}

}
Aug 27

ElementByName

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class ElementByName {
	WebDriver driver = null;

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
	}

	@Test
	public void testById() {
		driver.get("https://testautomation.co/webelements/");
        driver.findElement(By.name("yourname")).sendKeys("Mike");
        List<WebElement> names = driver.findElements(By.name("yourname"));
		names.get(1).sendKeys("Miky");
        
	}

	@AfterMethod
	public void afterMethod() {
		// driver.quit();

		
	}

}
Aug 27

RadioButtonExample

By testautomation | Selenium Code

package selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class RadioButtonExample {
	WebDriver driver;

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
	}

	@Test
	public void testRadioButton() {
		driver.get("https://testautomation.co/webelements/");
		List<WebElement> browserradios = driver.findElements(By.name("browser"));
		//browserradios.get(1).click();
		for(WebElement radio:browserradios)
		{
			System.out.println(radio.getAttribute("value"));
			if (radio.getAttribute("value").equals("ie"))
			{
				radio.click();
			}
				
		}
		
	}

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

}
Aug 27

WebDriverDemoByXPath

By testautomation | Selenium Code

package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverDemoByXPath {

	public static void main(String[] args) {

		System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		WebElement searchBox = driver.findElement(By.xpath("//input[@title='Search' and @type='text']"));
		searchBox.sendKeys("TestAutomation.co");
		WebElement submitSearchBtn = driver.findElement(By.name("btnK"));
		submitSearchBtn.submit();
//		
		driver.quit();

	}

}