I want to test the signup page wherein I have written a code that will give all the required details that are required for the website but after filling up all the required fields then it should click on the Sign Up button which will redirect to the next URL. Once, it's redirected to the next URL then the validation is passed else fail. To test the URL I am storing into a variable u but when I executed the code in the u I am getting the original URL only and not redirecting to the post URL.
Code:
package Seleniumtesting;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Selenium {
ChromeDriver driver;
String url ="https://login.mailchimp.com/signup/";
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
TimeUnit.SECONDS.sleep(2);
driver.manage().deleteAllCookies();
driver.get(url);
String urlFromWebpage = driver.getCurrentUrl();
if(urlFromWebpage.equals("https://login.mailchimp.com/signup/")) {
System.out.println("PASS");
}
else {
System.out.println("FAIL");
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public void signup(){
try {
WebElement createAccountHeading = driver.findElement(By.xpath("//span[text()='Create an account or ']"));
if(createAccountHeading.isDisplayed()) {
System.out.println("PASS");
}else
System.out.println("FAIL");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("username")).sendKeys("Testvide1243");
driver.findElement(By.name("password")).sendKeys("Test123@");
TimeUnit.SECONDS.sleep(2);
driver.findElement(By.name("marketing_newsletter")).click();
TimeUnit.SECONDS.sleep(2);
//driver.findElement(By.xpath("//button[@id='create-account']")).click();
//driver.findElement(By.xpath("//*[@id=\"create-account\"]")).click();
WebElement element = driver.findElement(By.xpath("//*[@id=\"create-account\"]"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
TimeUnit.SECONDS.sleep(10);
String u = driver.getCurrentUrl();
System.out.println("URL: " u);
/*if(u.equalsIgnoreCase("https://login.mailchimp.com/signup/success/"))
{
System.out.println("PASS !! Account created successfully");
}
else
{
System.out.println("FAIL !! It might have not met the criteria");
}*/
driver.close();
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
Selenium mc = new Selenium();
mc.invokeBrowser();
mc.signup();
}
}
CodePudding user response:
Looks like synchronisation issues. Avoid using the hardcoded waits. After clicking on the Sign up button, wait for an element expected to be in the next page before you get the next page's url as shown in the below code:
import java.time.Duration;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Signup {
static WebDriver driver;
public static WebElement findElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://login.mailchimp.com/signup/");
findElement(By.id("email")).sendKeys("[email protected]");
findElement(By.id("new_username")).sendKeys("Testvide12412321233");
findElement(By.id("new_password")).sendKeys("Test123@");
findElement(By.cssSelector("div#onetrust-close-btn-container button[aria-label='Close']")).click();
findElement(By.id("marketing_newsletter")).click();
findElement(By.id("create-account")).click();
findElement(By.xpath("//h1[text()='Check your email']"));
System.out.println(driver.getCurrentUrl());
driver.quit();
}
}
Demo
CodePudding user response:
Ideally before validating the CurrentUrl you need to induce WebDriverWait for visibilityOfElementLocated() for any of the visible element within the DOM Tree. As an example, waiting for the h1 with text as Welcome to Mailchimp to be visible as follows:
driver.get(url);
String urlFromWebpage = driver.getCurrentUrl();
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(., 'Welcome to Mailchimp')]")));
if(urlFromWebpage.equals("https://login.mailchimp.com/signup/")) {
System.out.println("PASS");
}
else {
System.out.println("FAIL");

