Login to a Web Application from Facebook credentials :- Automating using Selenium + Maven
Isn’t it a difficult and an annoying thing to remember different credentials for different web applications? So, why not only one password for all your apps? This can be achieved by using WSO2 Identity Server. A detailed article can be found in below link.
Today what I’m going to discuss is automating the above process using Selenium Web Driver.
Prerequisites
Download WSO2 Identity Server https://wso2.com/identity-and-access-management
IntelliJ IDEA https://www.jetbrains.com/idea/
Facebook application created in developer.facebook.com (Please follow the steps mentioned in the above document)
Set up Travelocity Sample : Here we use the Travelocity Web Application as a sample. https://docs.wso2.com/display/IS540/Logging+in+to+your+application+via+Identity+Server+using+Facebook+Credentials#LoggingintoyourapplicationviaIdentityServerusingFacebookCredentials-Deployingtravelocity.comsampleapp
Download Chrome Driver : https://sites.google.com/a/chromium.org/chromedriver/home
Let’s start Automation!
- Open IntelliJ Idea and create a Maven Project.
<groupId>WSO2is</groupId>
<artifactId>com.wso2.is</artifactId>

2. Add following dependencies to the pom file.
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
3. Create a class in src/test/java/ folder.
4. I have grouped this process to following structure.
BeforeClass
createSP
createIdpFacebook
selectFBasFedaratedAuthenticator
loginToTravelocity
tearDown
4. BeforeClass: under @beforeClass annotation I have scripted the login part to the Identity Server in order to create the Service Provider and the external Identity Provider which is Facebook.
Further, I have used an assertion to verify if login is successful.
@BeforeClass
public void loginToIS()
{
driver.get("https://localhost:9443/carbon/");
WebElement UserName = driver.findElement(By.id("txtUserName"));
UserName.clear();
UserName.sendKeys("admin");
WebElement password = driver.findElement(By.id("txtPassword"));
password.clear();
password.sendKeys("admin");
WebElement loginbtn = driver.findElement(By.cssSelector("input.button"));
loginbtn.click();
String actualTitle = driver.getTitle();
String expectedTitle = "WSO2 Management Console";
Assert.assertEquals(actualTitle,expectedTitle);
Assert.assertTrue(driver.getPageSource().contains("Welcome to the WSO2 Identity Server Management Console"));
}
Since I’m running this sample in Chrome Browser, you have to download and initiate Chrome Web Driver for Selenium. You have to download the web driver accordingly.
public WebDriver driver;
System.setProperty("webdriver.chrome.driver","<path to chromeDriver>");
driver = new ChromeDriver();
5. In create SP method, the steps of service provider creation are included. The assertion has been used to verify the process.
@Test
public void createSP(){
wait = new WebDriverWait(driver, 10);
WebElement addSp = driver.findElement(By.xpath("//*[@id=\"menu\"]/ul/li[3]/ul/li[8]/ul/li[1]/a"));
addSp.click();
WebElement spName = driver.findElement(By.id("spName"));
spName.clear();
spName.sendKeys("travelocity.com");
WebElement spDescription = driver.findElement(By.id("sp-description"));
spDescription.clear();
spDescription.sendKeys("This is the Service Provider");
WebElement registerBtn = driver.findElement(By.xpath(" //*[@id=\"add-sp-form\"]/div[3]/input[1]"));
registerBtn.click();
WebElement inboundAuth = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"app_authentication_head\"]/a")));
inboundAuth.click();
WebElement samlAuth = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"saml.config.head\"]/a")));
samlAuth.click();
WebElement saml_link = wait.until(ExpectedConditions.elementToBeClickable(By.id("saml_link")));
saml_link.click();
WebElement issuer = driver.findElement(By.id("issuer"));
issuer.clear();
issuer.sendKeys("travelocity.com");
WebElement assertionConsumerURLTxt = driver.findElement(By.id("assertionConsumerURLTxt"));
assertionConsumerURLTxt.clear();;
assertionConsumerURLTxt.sendKeys("http://localhost:8080/travelocity.com");
WebElement addAssertionConsumerURLBtn = driver.findElement(By.id("addAssertionConsumerURLBtn"));
addAssertionConsumerURLBtn.click();
WebElement yesbtn = driver.findElement(By.xpath("/html/body/div[3]/div[2]/button[1]"));
yesbtn.click();
WebElement enableAttributeProfileBtn = driver.findElement(By.id("enableAttributeProfile"));
enableAttributeProfileBtn.click();
WebElement updateSPBtn = driver.findElement(By.xpath("//*[@id=\"addServiceProvider\"]/table/tbody/tr[2]/td/input[1]"));
updateSPBtn.click();
WebElement sucessBtn_spcreate = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[3]/div[2]/button")));
sucessBtn_spcreate.click();
WebElement updateBtn_spcreate = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"configure-sp-form\"]/div[9]/div/input[1]")));
updateBtn_spcreate.click();
String actualSPname = driver.findElement(By.xpath("//*[@id=\"ServiceProviders\"]/tbody/tr/td[1]")).getText();
String expectedSPname = "travelocity.com";
Assert.assertEquals(actualSPname,expectedSPname);
}
6. In createIdpFacebook method, the external IDP Facebook has been created.
@Test
public void createIdpFacebook()
{
wait = new WebDriverWait(driver, 10);
WebElement addIdpBtn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"menu\"]/ul/li[3]/ul/li[10]/ul/li[1]/a")));
addIdpBtn.click();
WebElement idPName = driver.findElement(By.id("idPName"));
idPName.clear();
idPName.sendKeys("FacebookIdp");
WebElement idpDisplayName = driver.findElement(By.id("idpDisplayName"));
idpDisplayName.clear();
idpDisplayName.sendKeys("Facebook");
WebElement idPDescription = driver.findElement(By.id("idPDescription"));
idPDescription.clear();
idPDescription.sendKeys("User can use Facebook credentials to login to Travelocity using Facebook Idp");
WebElement out_bound_auth_head = wait.until(ExpectedConditions.elementToBeClickable(By.id("out_bound_auth_head")));
out_bound_auth_head.click();
WebElement fb_auth_head = wait.until(ExpectedConditions.elementToBeClickable(By.id("fb_auth_head")));
fb_auth_head.click();
WebElement fbAuthEnabled = wait.until(ExpectedConditions.elementToBeClickable(By.id("fbAuthEnabled")));
fbAuthEnabled.click();
WebElement fbClientId = driver.findElement(By.id("fbClientId"));
fbClientId.clear();
fbClientId.sendKeys("<clientId>");
WebElement fbClientSecret = driver.findElement(By.id("fbClientSecret"));
fbClientSecret.clear();
fbClientSecret.sendKeys("<clientSecret>");
WebElement fbUserInfoFields = driver.findElement(By.id("fbUserInfoFields"));
fbUserInfoFields.clear();
fbUserInfoFields.sendKeys("id,name,gender,email,first_name,last_name,age_range,link");
WebElement fbCallBackUrl = driver.findElement(By.id("fbCallBackUrl"));
fbCallBackUrl.clear();
fbCallBackUrl.sendKeys("https://localhost:9443/commonauth");
WebElement registerIdpbtn = driver.findElement(By.xpath("//*[@id=\"middle\"]/div[2]/input[1]"));
registerIdpbtn.click();
String actualIdpname = driver.findElement(By.xpath("//*[@id=\"idPsListTable\"]/tbody/tr/td[1]")).getText();
String expectedIdname = "FacebookIdp";
Assert.assertEquals(actualIdpname,expectedIdname);
}
7. In selectFBasFedaratedAuthenticator method, the Facebook is selected as the Federated Authenticator of Service Provider.
@Test
public void selectFBasFedaratedAuthenticator()
{
WebElement listSps = driver.findElement(By.xpath("//*[@id=\"menu\"]/ul/li[3]/ul/li[8]/ul/li[2]/a"));
listSps.click();
WebElement editSp = driver.findElement(By.xpath("//*[@id=\"ServiceProviders\"]/tbody/tr/td[3]/a[1]"));
editSp.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement outboundAuth = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"app_authentication_advance_head\"]/a")));
outboundAuth.click();
WebElement federatedRadiobtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("federated")));
federatedRadiobtn.click();
WebElement fed_idp = wait.until(ExpectedConditions.elementToBeClickable(By.id("fed_idp")));
fed_idp.click();
WebElement updateSpbtn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"configure-sp-form\"]/div[9]/div/input[1]")));
updateSpbtn.click();
}
8. Finally the login to Travelocity process has been automated. The assertion verification has been included in this step in order to verify if login is successful.
@Test
public void loginToTravelocity()
{
wait = new WebDriverWait(driver, 10);
driver.get(travelocityUrl);
WebElement clickHerelink = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"content-area\"]/div[2]/h2[1]/a")));
clickHerelink.click();
WebElement fbEmail = wait.until(ExpectedConditions.elementToBeClickable(By.id("email")));
fbEmail.clear();
fbEmail.sendKeys("madurangi@wso2support.com");
WebElement fbPassword = wait.until(ExpectedConditions.elementToBeClickable(By.id("pass")));
fbPassword.clear();
fbPassword.sendKeys("<FacebookPassword>");
WebElement loginbutton = driver.findElement(By.id("loginbutton"));
loginbutton.click();
Assert.assertTrue(driver.getPageSource().contains("Travelocity.COM"));
Assert.assertTrue(driver.getPageSource().contains("You are logged in as madurangi@wso2support.com"));
}
You can find the full source code from : https://github.com/isuruuy429/SeleniumAutomation/blob/master/Testing%20IAM/src/test/java/LogintoTravelocitywithFB.java