Created
December 14, 2021 16:13
-
-
Save RDayal11/1ac49b9b0f4b565d98fd4c76e061eb28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package LambdaTest; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.testng.Reporter; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.time.Duration; | |
public class automateCaptchaInSelenium { | |
public String username = "YOUR USERNAME"; | |
public String accesskey = "YOUR ACCESSKEY"; | |
public static RemoteWebDriver driver = null; | |
public String gridURL = "@hub.lambdatest.com/wd/hub"; | |
@BeforeClass | |
public void setUp() throws Exception { | |
DesiredCapabilities capabilities = new DesiredCapabilities(); | |
capabilities.setCapability("browserName", "chrome"); | |
capabilities.setCapability("version", "95.0"); | |
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one | |
capabilities.setCapability("build", "CaptchaInSelenium"); | |
capabilities.setCapability("name", "TCaptchaInSeleniumSample"); | |
try { | |
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); | |
} catch (MalformedURLException e) { | |
System.out.println("Invalid grid URL"); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
driver.get("https://old.reddit.com/login"); | |
} | |
@Test | |
public void clickOnRecaptchaCheckBoxInSelenium() { | |
try { | |
System.out.println("Let's start with fresh registration"); | |
WebElement username=driver.findElement(By.xpath("//input[@name='user']")); | |
username.sendKeys("some_username_200"); | |
WebElement password=driver.findElement(By.xpath("//input[@name='passwd']")); | |
password.sendKeys("SuperStrongP@ssw0rd"); | |
WebElement verifyPassword=driver.findElement(By.xpath("//input[@name='passwd2']")); | |
verifyPassword.sendKeys("SuperStrongP@ssw0rd"); | |
WebElement email=driver.findElement(By.xpath("//input[@name='email']")); | |
email.sendKeys("xyz@gmail.com"); | |
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(25)); | |
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt( | |
By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); | |
wait.until(ExpectedConditions.elementToBeClickable( | |
By.xpath("//div[@class='recaptcha-checkbox-border']"))).click(); | |
System.out.println("Clicked the checkbox"); | |
} catch (Exception e) { | |
} | |
} | |
@Test | |
public void manuallySolveCaptchaWithDelayInSelenium() { | |
try { | |
System.out.println("Let's start with fresh registration"); | |
WebElement username=driver.findElement(By.xpath("//input[@name='user']")); | |
username.sendKeys("some_username_200"); | |
WebElement password=driver.findElement(By.xpath("//input[@name='passwd']")); | |
password.sendKeys("SuperStrongP@ssw0rd"); | |
WebElement verifyPassword=driver.findElement(By.xpath("//input[@name='passwd2']")); | |
verifyPassword.sendKeys("SuperStrongP@ssw0rd"); | |
WebElement email=driver.findElement(By.xpath("//input[@name='email']")); | |
email.sendKeys("xyz@gmail.com"); | |
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(25)); | |
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt( | |
By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); | |
wait.until(ExpectedConditions.elementToBeClickable( | |
By.xpath("//div[@class='recaptcha-checkbox-border']"))).click(); | |
System.out.println("Clicked the checkbox"); | |
wait.until(ExpectedConditions.elementToBeClickable( | |
By.xpath("//button[text()='sign up']"))).click(); | |
System.out.println("Clicked the sign up button"); | |
} catch (Exception e) { | |
} | |
} | |
@AfterClass | |
public void closeBrowser() { | |
driver.close(); | |
Reporter.log("Closing the browser", true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>LambdaTest</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-api</artifactId> | |
<version>4.0.0-alpha-7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-remote-driver</artifactId> | |
<version>4.0.0-alpha-7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-chrome-driver</artifactId> | |
<version>4.0.0-alpha-7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.testng</groupId> | |
<artifactId>testng</artifactId> | |
<version>6.14.3</version> | |
</dependency> | |
<dependency> | |
<groupId>io.github.bonigarcia</groupId> | |
<artifactId>webdrivermanager</artifactId> | |
<version>4.4.3</version> | |
</dependency> | |
</dependencies> | |
<properties> | |
<maven.compiler.source>8</maven.compiler.source> | |
<maven.compiler.target>8</maven.compiler.target> | |
</properties> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> | |
<suite name="automateCaptchaInSeleniumSuite"> | |
<test name="automateCaptchaInSeleniumTest"> | |
<classes> | |
<class name="com.infa.dqcloud.testcases.profiling.automateCaptchaInSelenium"> | |
</class> | |
</classes> | |
</test> | |
</suite> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment