Commit b2bd1e4c authored by Qazi Zain's avatar Qazi Zain

Initial commit to practice custom locators in Selenium

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
# Ignore IDE files
.idea/
# Ignore pom.xml file (if it's not required for version control)
pom.xml
# Ignore build directory (target)
target/
# Ignore src/main directory (if you want to ignore specific files/folders in src/main)
src/main/
package Locaters_Practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class AllTypeLocators {
public static void main(String[] args) throws InterruptedException {
WebDriver obj = new FirefoxDriver();
//addinng waiting mechanisim to avoid to fail due to timeout issue.
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
obj.get("https://rahulshettyacademy.com/locatorspractice/"); // to open that url.
// using id locators.
obj.findElement(By.id("inputUsername")).sendKeys("Zain");
// using name locator.
obj.findElement(By.name("inputPassword")).sendKeys("123456");
// using css locator.
obj.findElement(By.className("signInBtn")).click(); // tag name.className
// extracting text using locator.
String text = obj.findElement(By.className("error")).getText(); // if we will not add implicit time it will not get bcz sprit emd time is less then its display time.
System.out.println("Extracted text is:"+text);
// linktext locator.
obj.findElement(By.linkText("Forgot your password?")).click();
//using locator as css selector
obj.findElement(By.cssSelector("input[placeholder='Name']")).sendKeys("Qazi");
// using locator as xpath selector.
obj.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys("k201038@nu.edu.pk");
// clearing the email box using xpath absolute path.
obj.findElement(By.xpath("//form/input[2]")).clear();
// Re entering value of email using css selector regular expression.
obj.findElement(By.cssSelector("input[placeholder*='Em']")).sendKeys("qazizain@gmail.com");
//Entering value of phone no using xpath regular expression.
obj.findElement(By.xpath("//input[contains(@placeholder,'Phone')]")).sendKeys("03313467894");
WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(20));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.overlay-panel.overlay-right")));
// clicking on reset button
obj.findElement(By.cssSelector("button[class*='reset']")).click();
String pass= obj.findElement(By.cssSelector("p.infoMsg")).getText();
String password = pass.split("'")[1];
System.out.println("Password is:"+password);
obj.findElement(By.cssSelector("button[class*='login']")).click();
obj.findElement(By.cssSelector("input#inputUsername")).sendKeys("Qazi");
obj.findElement(By.cssSelector("input[placeholder='Password']")).sendKeys(password);
Thread.sleep(2000); // using it to make elements clickable.
obj.findElement(By.cssSelector("input#chkboxOne")).click();
obj.findElement(By.cssSelector("input#chkboxTwo")).click();
obj.findElement(By.cssSelector("button[type='submit']")).click();
String dilouge = obj.findElement(By.xpath("//div[@class='login-container']/h2")).getText();
System.out.println("After login :"+dilouge);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment