Unable to delete all cookies in selenium - java

Configurations:
OS : Ubuntu
Java version : 8
Selenium : 3.8.0
ChromeDriver : 2.33
Chrome version : 60
I'm automating an application where i have to login in a system using Social media
After entering mobile number it ask for password and there social media icon. If i click lets say google plus, it opens google plus login window then I have to provide credential then the window get closed automatically and user gets login. As shown here
My issue is, after logout from my application if i again try to login using google plus then it perform automatic login (second time it won't ask for google plus credentials as google plus session cookies are there)
So in my script I'm trying to delete all cookies after first test but it is not working code line I've tried :
driver.manage().deleteAllCookies();
For reference complete test look like :
#Test
public void newUserRegistration_GooglePlus_ForAlreadyExistEmail() throws IOException, InterruptedException, JSONException
{
LogoutAnalyzer.checkLogout();
loginpage.sendUsername(excel.getCellData(6, 18, 1));
CommonMethods.waitUntilLoaderGetInvisible(driver);
TakeScreenshot.passedScreenShot("Username");
LogWriter.logger.info("Mobile number has entered");
loginpage.clickSubmitButton();
CommonMethods.waitUntilLoaderGetInvisible(driver);
LogWriter.logger.info("Submit button clicked");
if(loginpage.otpTextbox.size()>0)
{
String OTP = otpreader.getOTP(excel.getCellData(6, 18, 1));
LogWriter.logger.info("Got the OTP via API call OTP is "+OTP);
loginpage.enterOTP(OTP);
TakeScreenshot.passedScreenShot("OTP_Entered");
LogWriter.logger.info("OTP Entered");
loginpage.clickConfirmCodeButton();
CommonMethods.waitUntilLoaderGetInvisible(driver);
LogWriter.logger.info("Submit button clicked");
TakeScreenshot.passedScreenShot("Profile_Info");
loginpage.clickGooglePlusIcon();
Thread.sleep(5000);
LogWriter.logger.info("Switch to Google Plus window");
CommonMethods.switchWindow(driver);
CommonMethods.waitForElementVisibility(driver, new RCONGooglePlusPage(driver).usernameField);
TakeScreenshot.passedScreenShot("Google Window");
sm_helper.googlePlusLogin(excel.getCellData(6, 18, 2), excel.getCellData(6, 18, 3));
CommonMethods.switchToParentWindow(driver);
LogWriter.logger.info("Switched back to RContacts window");
CommonMethods.waitUntilLoaderGetInvisible(driver);
Assert.assertEquals(loginpage.getValidationMessage(), PropertyFileReader.getProperty("emailAlreadyExistMessage"));
LogWriter.logger.info(PropertyFileReader.getProperty("emailAlreadyExistMessage"));
}
else
{
TakeScreenshot.passedScreenShot("Already_registered_User");
LogWriter.logger.info("This user is already a registered user");
}
driver.manage().deleteAllCookies();
LogWriter.logger.info("all cookies deleted");
}

Related

Hibernate - Tomcat - SQL Server - Connection closed issue

I've a webapp running on TOMCAT 7.0.5 - JDK 1.7 - and connecting to SQL Server 2012.
The hibernate version is 4.3.5. Browser used is IE 11.0.
The issue Is I have a webpage: A normal FORM submit (submit.jsp). It shows a readonly page (submitConfirm.jsp) and has two buttons.
Submit - Saves the application and redirects the page to the home page to start subscription again
Print - opens a new tab via javascript to print a PDF form that likely takes 2 to 5 seconds to render in a "new" tab.
The submit button is disabled by defualt and enabled only when user clicks on the PRINT Button. While the item is still rendering If someone clicks on Continue button - all actions perform well and at the I get both the print and subscriptions saved.
The next subscriptions entered would throw a http 500 error indicating the "Connection is already closed".
I use the Standard HibernateUtil paradigm to begin/end/commit/rollback transactions.
The models are all XML based (not entity decorator based).
For normal workflows where if the user waits till the PDF is rendered all is fine.
But If the user is pressing the button of continue - Seems there is an issue.
Attached is the screen-grab of the error is someone finds it useful provide some hints.
Based on the user feedback (the concerned method):
public String reportSessionId(String userId) throws InfrastructureException {
String sid = null;
try {
sid = (String) HibernateUtil.getSession()
.getNamedQuery("com.ttsme.ims.model.IMSUser.reportSessionId")
.setString("userId", userId).uniqueResult();
}
catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
catch (Exception exp)
{
exp.printStackTrace();
}
return sid;
}

Keycloak is not maintaining session in browser

I am using keycloak(4.3.0) openid-connect flow. For my client app, I am using:
public class AuthenticationTest {
public static void main(String[] args) {
final String KEYCLOAK_JSON = Constants.KC_CONFIG_JSON_PATH;
InputStream config = Thread.currentThread().getContextClassLoader().getResourceAsStream(KEYCLOAK_JSON);
KeycloakInstalled keycloak;
try {
keycloak = new KeycloakInstalled(config);
config.close();
keycloak.loginDesktop();
AccessToken token = keycloak.getToken();
System.out.printf("UserId: %s%n", token.getSubject());
System.out.printf("Username: %s%n", token.getPreferredUsername());
System.out.printf("Email: %s%n", token.getEmail());
System.out.println(keycloak.getTokenString());
System.in.read();
} catch (IOException | VerificationException | OAuthErrorException | URISyntaxException | HttpFailure
| InterruptedException e) {
e.printStackTrace();
}
}
}
I have referred the example mentioned here: https://gist.github.com/thomasdarimont/ca16080145d226e50628d5696ffb9508
First time I launch the application and login, I get
"Login Successful.You may close this browser window and go back to
your console application."
However, if I launch second instance of my application/ relaunch my application even without closing the previous browser window , still the login page appears instead of you are already logged in message.
(The new login page is appearing in a new tab on the same window as the first login page, so it is not an issue of failure to maintain state across different windows. Also I have tried relaunching the app withing 20 seconds so it does not seem t be an issue of timeout either)
Browser: Firefox, chrome.
Any idea what I am doing wrong. Expectation is that browser must maintain a keycloak session for the same user until closed for at least a few minutes.
NOTE: I did not face this issue in Keycloak 3.4.3. But it started coming on 4.1.0 and 4.3.0
I see follwing difference in cookies on 3.4.3 and 4.x:
3.4.3: (same whther first login or subsequent login)
4.x First time login:
4.x Subsequent login: (an addition KC_RESTART cookie is seen)

How to handle switching from mobile chrome browser to android native like sms, google playstore and vice-versa?

Selenium : 3.0.0
chromedriver : 2.36
Appium : 1.7.2
Android Chromebrowser : 65.0.32..
Requirement:
Scenario1:
1.Open mobile browser page
2.Click on one button
3.Now the android Google playstore screen should be displayed.
4.Comeback to the mobile browser again.
Scenario2:
1.Open mobile browser page
2.Click on one button
3.Now the android SMS should be displayed.
4.Comeback to the mobile browser again.
public class KindleModule extends LaunchApp {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
// TODO Auto-generated method stub
LaunchApp app = new LaunchApp();
AndroidDriver<AndroidElement> driver=capabilities();
//WebDriver driver;
driver.get("https:abc");
System.out.println("application launched");
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='abc']/div/")).click();
Thread.sleep(2000);
//after clicking this button i want to navigate to playstore because example kindle app is not present and after that i want to come back:
driver.findElement(By.xpath(".//*[#id=abcPopup']/button")).click();
Activity activity = new Activity("appPackage", "appActivity");
//redirect to the Activity
driver.startActivity(activity);
}
}
Can someone please help how to do this please?
I have also got the same issue when I stared mobile automation. After research, I got a solution and it's working fine.
Try below example, I think it should work for you.
Solutions for your both scenarios:
Open mobile browser page
Click on one button
Now the android Google playstore screen should be displayed.
//Wait until Google playstore screen not appear. I'm using Thread.sleep() method due to some performance issue.
Thread.sleep(2000);
//Comeback to the mobile browser again. For that you have to use appPackage and appActivity which you want to open name as below.
//e.g. Activity activity = new Activity("com.test.app", "com.test.app.MainActivity");
Activity activity = new Activity("com.android.chrome", "com.android.chrome.app.Main");
//redirect to the Activity
driver.startActivity(activity);
System.out.println("app opened");
Please let me know if you have any concerns related to this.

Jenkins can't login on Google accounts from Selenium FireFox webdriver

I have a major problem which I can't find a fix for a few days. I made an automated test in Java which logins on Google Gmail. I integrated the test with Jenkins and when it runs it's get stucked when he has to enter the Password. So he enters the user credentials in the correct field, he clicks the next button but in the next screen where the Password field should be present, Jenkins doesn't see it. I tried to locate the element and see if it's present and nothing, It's like it doesn't actually doesn't reach that page.
If I run the test normal from Eclipse there are no issue present, the test runs well so the elements are identified properly.
Please help me, thanks
public static void sendEmail() throws InterruptedException{
driver.get("http://accounts.google.com");
WebElement element2 = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("Email")));
System.out.println("email is present");
driver.findElement(By.id("Email")).sendKeys("my email#gmail.com");
driver.findElement(By.id("next")).click();
Thread.sleep(1000);
System.out.println("user on");
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
System.out.println("Element is present");
driver.findElement(By.id("Passwd")).sendKeys("my pass");
System.out.println("pass on");
driver.findElement(By.id("signIn")).click();
System.out.println("clicked on sign on");
Firstly Google Blocks testexecutions on in its products say gmail or youtube. to do this. enable Less Secure App Access and then try it out. also. to properly mimic the user behaviour pass user agents to via scripts.

FB SDK not working on OS 7

I am implementing FB in one of my app.I am using jar 0.8.25. Its working fine on all simulators from 5 to 7.1.And for devices works only for OS 5 and 6 but not working on device 7 and 7.1.For OS 7 after log in success it remains on FB page it doesn't redirect back. and when i press back button, i get error encountered unable to refresh access token with try again button.
When analyzing on console it never finds access token single time for OS 7.while for 5 and 6 its working perfectly.
Please tell what may cause the issue.
Thanks,
This isn't a solution to your specific problem. I mentioned in the comments that I'm using an interface. So I'm posting here as its too much for the comment section. It is also not the COMPLETE solution, you will need to handle the flow and expired tokens, this is just to show you the logic of how I did this.
For my interface I open a browserfield to the Oauth url:
https://www.facebook.com/dialog/oauth?client_id=<APP_ID>&response_type=token&redirect_uri=http://www.facebook.com/connect/login_success.html&scope=publish_actions
And I add a listener to this browser to listen for the redirects after login. Once you have the access token, you should persist it and close the browserfield.
private class OAuthScreen extends MainScreen
{
BrowserField browser_field;
LoadingDialog loading_dialog;
public OAuthScreen(final Command task)
{
super(VERTICAL_SCROLL | HORIZONTAL_SCROLL);
BrowserFieldConfig browserConfig = new BrowserFieldConfig();
browserConfig.setProperty(BrowserFieldConfig.VIEWPORT_WIDTH, new Integer(Display.getWidth()));
browser_field = new BrowserField(browserConfig);
browser_field.addListener(new BrowserFieldListener()
{
public void documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) throws Exception
{
int index = browserField.getDocumentUrl().indexOf("#access_token=");
if (index == -1)
{
super.documentCreated(browserField, scriptEngine, document);
}
else
{
access_token = browserField.getDocumentUrl().substring(index + "#access_token=".length(), browserField.getDocumentUrl().indexOf("&"));
PersistentObject store = PersistentStore.getPersistentObject(STORE_KEY);
FacebookTokens store_tokens = new FacebookTokens();
store_tokens.access_token = access_token;
store.setContents(store_tokens);
store.commit();
if (task != null) task.execute();
OAuthScreen.this.close();
}
}
public void documentLoaded(BrowserField browserField, Document document) throws Exception
{
super.documentLoaded(browserField, document);
loading_dialog.close();
}
});
// whatever loading dialog you want, this sometimes takes a while to open
loading_dialog = LoadingDialog.push(loading_field);
add(browser_field);
browser_field.requestContent("https://www.facebook.com/dialog/oauth?client_id="+APP_ID+"&response_type=token&redirect_uri=http://www.facebook.com/connect/login_success.html&scope=publish_actions");
}
}
The callback task is just for if I want to perform a call directly after login.
Now just perform API calls as you need them. API methods here https://developers.facebook.com/docs/graph-api/reference/v2.0/
Methods that require the access token, should have it appended to the url such as, https://graph.facebook.com/me/feed?access_token=" + access_token
Be aware that clearing your access token won't clear the token stored in the browser field. And will mean that you can't login next time (because the browser is still logged in).
So if you want to logout you need to open this link in a browserfield before clearing your local access token "https://www.facebook.com/logout.php?next=http://www.facebook.com/connect/login_success.html&access_token=" + access_token
Clearing the cookies of the browser should suffice, but I haven't found a way to do this.

Categories

Resources