1

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.Keys;
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 {
            driver.manage().window().maximize();
        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("Testvidewedrq12435@gmail.com");
        driver.findElement(By.name("username")).sendKeys("Testvidwqrdee1243");
        driver.findElement(By.name("password")).sendKeys("Test123");
        TimeUnit.SECONDS.sleep(2);
        driver.findElement(By.name("marketing_newsletter")).click();
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
        TimeUnit.SECONDS.sleep(2);
        
        driver.findElement(By.xpath("//button[@id='create-account']")).click();
        
        TimeUnit.SECONDS.sleep(5);
        
        driver.findElement(By.xpath("//h1[text()='Check your email']"));
       // System.out.println(driver.getCurrentUrl());
        String u = driver.getCurrentUrl();
        System.out.println("URL: "+u);
        
        if(u.contains("success"))
        {
            System.out.println("PASS !!");
        }
        else
        {
            System.out.println("FAIL !!");
        }
        driver.close();
        }
        catch(InterruptedException ex)
          {
              ex.printStackTrace();
          }
        
    }
    
    public static void main(String[] args) {
        Selenium mc = new Selenium();
        mc.invokeBrowser();
        mc.signup();
        
    }
    

}
Vicky
  • 639
  • 3
  • 13

1 Answers1

0

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");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352