Set User Agent in Playwright is not working - c#

I am trying to set the user agent of playwright browser while initializing the browser context but it seems that it ignores the param I am giving and starts it with the default agent
could anyone point out what is the problem here?
version : playwrightSharp v0.180.0
private static Task<IPage> InitializePlayWrightPage()
{
var page = MarketsHelper.browser.NewPageAsync(userAgent:Guid.NewGuid().ToString());
return page;
}

Related

Selenium Microsoft Login “Your browser is a bit unusual…” Message

I'm trying to do some UI testing with Selenium and NUnit in C# but need to login through Microsoft first. After entering my information with selenium, when I try to click Sign-In I get a popup saying:
"Your browser is a bit unusual... Try disabling ad blockers and other extensions, enabling javascript or using a different web browser."
My best guess is that it is detecting that I'm using selenium, but I'm not sure as I can't find anyone else with a similar message. Is this what is happening? If so, is the solution described in the link my best option or is there another way to login to Microsoft?
Here is a rough example of my code:
[TestFixture]
public class FileUploadTests
{
IWebDriver webDriver;
[OneTimeSetUp]
public void StartChrome()
{
var options = new ChromeOptions();
options.AddArgument("--enable-javascript");
options.AddArguments("--incognito");
options.AddArgument("--profile-directory=Default");
options.AddArgument("--disable-plugins-discovery");
options.AddArgument("--profile-directory=Default");
options.AddArgument(".");
webDriver = new ChromeDriver(options);
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}
[Test]
public void Login()
{
webDriver.Navigate().GoToUrl("https://localhost:xxxxx/");
webDriver.FindElement(By.Id("LoginButton")).Click();
webDriver.FindElement(By.Id("i0116")).Clear();
webDriver.FindElement(By.Id("i0116")).SendKeys("username");
webDriver.FindElement(By.Id("i0116")).SendKeys(Keys.Enter);
webDriver.FindElement(By.Id("password")).Clear();
webDriver.FindElement(By.Id("password")).SendKeys("password");
System.Threading.Thread.Sleep(500);
webDriver.FindElement(By.Id("password")).SendKeys(Keys.Enter);
webDriver.FindElement(By.Id("submitBtn")).Click();
}
[OneTimeTearDown]
public void CloseTest()
{
webDriver.Close();
webDriver.Dispose();
webDriver.Quit();
}
}
I was able to bypass logging in by using an opened browser where I logged in manually. Still not sure what was causing the original problem though.

Selecting web element with id in appium test started using AndroidDriver

I'm new to Appium and testing as a whole. I used Android driver to test an app in android.
I can handle all clicks and typing in the app. But the issue arises while using the OAuth service to log in the app.
I use the following Driver client. appium dotnet driver
The login service opens in a separate chrome browser. I have to type text in an input element inside the web page.
I use the following code. On debugging page resource only has the android elements of the chrome browser and nothing of the web content.
var appiumDriver = new AndroidDriver<AndroidElement>(driverUri, appiumOptions);
[Test()]
[Order(1)]
public async Task TestLogin()
{
try
{
appiumDriver.FindElementByAccessibilityId("Login").Click();
await Task.Delay(3000);
var source = appiumDriver.PageSource;
var element = appiumDriver.FindElementById("login-email");
}
catch (NoSuchElementException ex)
{
}
}
Can anyone suggest how to proceed with this problem? This also has to be done in iOS, I guess both can be handled the same way.. Thanks in advance.
The issue was not changing to web context for searching in web page. The test flow had to be
Perform action to open the web page from the app. (In my case clicking login button)
Wait till the web page appears. (This is important as the web context will be available only after the web page appears) For this, I used a FindElementByClassName for a unique control visible in the web page but not on the previous App page. There sure can be a better way.
Get available contexts and switch to web context.
I used FindByCssSelector for finding the element with id.
Changing to web context
public static void ChangeToWebContext()
{
if (driver.Context != "NATIVE_APP")
return;
var elements = driver.FindElementByClassName("android.widget.EditText");
var availableContext = driver.Contexts;
if (availableContext.Count >= 2)
{
driver.Context = availableContext[1];
return;
}
}
Usgae:
this.ChangeToWebContext();
driver.FindElementByCssSelector("#login-password");

Does anyone else have issues losing session variables in Firefox but not in Chrome or IE?

We are experiencing some issues with returning variables stored in session, which in turn is causing our controller authorization code to redirect some users back to the login page. The users only seem to be having this issue with the Firefox browser and the application works normally for them when using Chrome or Edge. It's almost as if their session is being dropped while navigating through the site (mostly immediately after login while being redirected to the home page, however sometimes the user can get through few links before being kicked out).The issue also only happens after if is running in our test or production sites, reproducing the issue locally troublesome. I was wondering if anyone else has experience this type of issue before and if they were able to find a work around or resolve it. Our sessionstate is set to InProc.
Based on other threads I have read on here I have tried the following so far:
Ensured that Session_Start is in Global.asax, Tried setting a "dummy" session variable in the Session_Start method "Session["Init"] = 1", added this to our web.config file under the section
, as well as some other solutions I have found that turned out not to resolve our issue.
This is the section of code in question.
public class AuthorizeSessionAttribute : AuthorizeAttribute, IAuthorizationFilter
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
HttpSessionStateBase session = filterContext.HttpContext.Session;
Controller controller = filterContext.Controller as Controller;
if (controller != null && session != null && session["CurrentRole"] == null)
{
controller.HttpContext.Response.Redirect("~/Account/Login");
}
base.OnAuthorization(filterContext);
}
}
I just can't see any obvious reason as to why this would only happen in Firefox but work correctly in Chrome or IE. Any input on this would be appreciated. Thanks.

IdentityServer4 and Angular: logout with Implicit flow

I have an Angular app that integrates with IdentityServer4 with implicit flow and the angular-oauth2-oidc library.
Everything seems to work fine, I can log in; and access token is available.
If I click the logout button, I handle it like:
logout() {
this.oauthService.logOut();
}
...and I'm redirected to Identity Server, where it asks me if I really want to log out.
My question is whether I can bypass that prompt? I mean, I want to log out completely if the button is clicked and redirected back to the Angular site, without the need to confirm it?
How can this be achieved?
EDIT: as mentioned in the other answers, it should work if you pass id_token_hint. So I did:
logout() {
this.oauthService.customQueryParams = {
'id_token_hint': this.oauthService.getIdToken()
};
this.oauthService.logOut();
}
But it doesn't make any difference.
There were two issues I needed to fix in order to make this work.
In IdentityServer, AccountOptions class, I had to set this property to true instead of false:
public static bool AutomaticRedirectAfterSignOut = true;
Next, In IdentityServer client configuration, I had to define the post logout redirect uri:
PostLogoutRedirectUris = new List<string> {"http://...."}
That did it. I did not have to change anything in the Angular client.

Navigating to another pages using navigation service with MVVM from the constructor

I'm developing a universal app using MVVM (light) and I'm trying to integrate OneDrive using the SDK. Each of my pages have a ViewModel associated with them.
I have a start up page (OneDrivePromptPage) where I offer the user to either sign in to Microsoft OneDrive or skip the sign in process.
When the user clicks on the Skip button, it calls the relevant command in my OneDrivePromptViewModel and navigates to my MainPage by calling:
this._navigationService.Navigate(typeof(MainPage));
The same applies when the user clicks on the Sign button and it prompts the user for their OneDrive credentials and if successfully logged in, it navigates to my 'MainPage'.
Now, here's my problem. When signs in successfully to OneDrive, I set a flag (IsSignedIn=true) to a container and save it to my storage (RoamingSettings).
All of the above works fine but I'm facing a problem where when I start the application again, it checks OneDrivePromptViewModel's constructor if the IsSignedIn value from the storage is set to true. If it is, I'll try to automatically sign in to OneDrive. If successful, I want to navigate to my 'MainPage' but this won't work.
It calls the same navigate code as above, doesn't throw any errors but it doesn't navigate to my 'MainPage' and ends up displaying my "Prompt" page.
public OneDrivePromptViewModel(INavigationService navigationService,
ISettingsDataService settingsDataService)
{
this._navigationService = navigationService;
this._settingsDataService = settingsDataService;
bool isSignedIn = false;
isSignedIn = this._settingsDataService.
GetItem<bool>("MyStoreApp", "IsSignedIn");
if (isSignedIn && !MyOneDrive.IsSignedIn())
{
ExecuteSignInCommand();
}
else if (isSignedIn && MyOneDrive.IsSignedIn())
{
NavigateToMainPage();
}
}
Any ideas why it's not navigating to the relevant page. Is it because I'm calling this from the constructor. If that's not the correct place to call it from, where should I call this from?
I'm not even sure, I should be loading the OneDrivePromptPage? Should I be performing this checks in a method of some sort before loading either OneDrivePromptPage or MainPage and perform the same checks mentioned above, but where do I put this method? In my App.cs?
Thanks.
In a recent Universal app I check for something similar, but initiate this in the OnLaunched event of the App.xaml.cs
Making sure based on the isSignedIn check that I navigate to the correct page like so:
bool navigated = rootFrame.Navigate(isSignedIn ? typeof(MainPage) : typeof(PromptPage);
You could try to see if this also works in your scenario

Categories

Resources