So I seem to be having issues accessing a public boolean from one .cs file in another within the same project.
Both are within the same project and namespace, the boolean as below is declared as public yet when I try to check if true in the second file it says it is not defined.
I define the boolean in the code below:
public bool CXML_ProjectLoaded()
{
if (CXML_CreateProjectFiles_EmptyFieldCheck() == false)
{
return true;
}
else
{
return false;
}
}
In my second file I then try to use it as an if true:
public void CXML_ProjectLoadVisibility()
{
if (CXML_ProjectLoaded() == false)
{
// Remove the Tabs.
// Project Overview
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_ProjOver);
//Kingdoms
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Kingdom);
//Clans
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Clans);
//Lords
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Lords);
//Cultures
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Cultures);
//Settlements
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Settlements);
//Items
CXML_MainPage_TabControl_Main.Items.Remove(CXML_TabHeader_Items);
}
else if (CXML_ProjectLoaded() ==true)
{
// Add back the tabs
// Project Overview
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_ProjOver);
//Kingdoms
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Kingdom);
//Clans
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Clans);
//Lords
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Lords);
//Cultures
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Cultures);
//Settlements
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Settlements);
//Items
CXML_MainPage_TabControl_Main.Items.Add(CXML_TabHeader_Items);
}
else
{
System.Windows.Forms.MessageBox.Show("Broken Stuff!");
}
}
However this is where VS says CXML_ProjectLoaded doesn't exist.
As per the below screenshot they are both in the same project:
Link
Realised I needed to do Window1.Boolean instead :)
Related
I'm trying to create a script where I can check if the element is or is not displayed on the UWP app. I have a methods that checks if the element is Displayed or Not Displayed..
if the element is actually displayed and I called the "IsElementDisplayed", the test seems to works fine and continue the test execution.. But when the element is really NOT displayed and I called the "IsElementDisplayed", It doesn't return false boolean value.. but it just stops the test execution and says, "no such given parameters found"...
Please check my sample code....
I have a class which contains my locators and returns the instance of WindowsElement:
protected WindowsElement Id(string id)
{
return Driver.FindElementById(id);
}
protected WindowsElement XPath(string xpath)
{
return Driver.FindElementByXPath(xpath);
}
protected WindowsElement Name(string name)
{
return Driver.FindElementByName(name);
}
protected WindowsElement AccessibilityId(string id)
{
return Driver.FindElementByAccessibilityId(id);
}
and then I have Page class which contains properties of my elements.. sample code below:
public WindowsElement SaveObligation_Btn => this.AccessibilityId("OBLGTN_saveObligation_btn");
public WindowsElement CancelObligation_Btn => this.AccessibilityId("OBLGTN_CancelObligation_btn");
public WindowsElement ObligationAdd_Btn => this.AccessibilityId("SMMRY_AddObligation_btn");
And lastly I have a testhelper class that contains methods such as:
public bool IsNotDisplayed(WindowsElement element)
{
try
{
Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
return false;
}
catch (Exception)
{
return true;
}
}
but when I trying to call the "IsNotDisplayed" method to return false if it caught any exceptions.., My test execution stops and I will have an error that is pointing to my Locator class and says, the "element is NOT found with the given parameters"...
I expect the method "isNotDisplayed" should return false boolean value, so I can validate if the element is visible or not.
I'm not familiar with C#.
But in Python, I will write like this:
try:
element = //find your element
if element.is_displayed():
return True
else:
raise AssertionError
except Exception:
return False
first question is what you want to do when noElementIsDisplayed? Of course, it will catch some exceptions. In your scenario, if you do not want to do anything when exception thrown then you should leave catch block empty.
try the following code:
public bool IsNotDisplayed(WindowsElement element)
{
try
{
Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
return false;
}
catch (Exception e)
{
// do nothing and it will continue your script.
}
}
This will return true if the element is displayed else it will return false.
C# Code
public bool IsNotDisplayed(WindowsElement element)
{
try
{
element.Displayed;
}
catch (Exception)
{
return false;
}
return true;
}
Java Code
public static boolean IsDispayed(WebElement webelement)
{
try {
webelement.isDisplayed();
} catch (Exception e) {
return false;
}
return true;
}
Python Code
try:
element = driver.find_element_by_xpath('XPATH')
if element.is_displayed():
return True
else:
raise AssertionError
except Exception:
return False
Try the below code for c#. This is an alternative..
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
I have an issue finding i good way of checking if an element is present.
I´m using Page Object Model when automating my test cases meaning i declare all elements in a specific class, and not in the actual [tests]. How can i transform this simple method for verifying declared elements like this:
private IWebElement LoginButton => driver.FindElement(By.Id("LoginButton"));
IsElementPresent(IWebElement element)
{
try
{
//Something something
}
catch(NoSuchElementException)
{
return false;
}
return true;
}
I had a similar issue not long ago:
Managed to include a retry strategy/policy in it, so I'm waiting for the element to exist in the DOM.
public static void WaitForElementToExistInDom(Func<IWebElement> action)
{
RetryPolicy.Do(() =>
{
if (!DoesElementExistInDom(action))
throw new RetryPolicyException();
}, TimeSpan.FromMilliseconds(Timeouts.DefaultTimeSpanInMilliseconds), TestConstants.DefaultRetryCount);
}
public static bool DoesElementExistInDom(Func<IWebElement> action)
{
var doesExist = false;
try
{
var element = action.Invoke();
if (element != null)
doesExist = true;
}
catch (StaleElementReferenceException)
{
}
catch (NullReferenceException)
{
}
catch (NoSuchElementException)
{
}
return doesExist;
}
And you can call it like this:
WebDriverExtensionMethods.WaitForElementToExistInDom(() => Map.YourElement);
If the element is stalled or not existing, internally we're going to handle the exceptions and try again.
And because the evaluation 'if the element exists in the DOM' is done when you're getting calling the element from the MAP, we're wrapping it in an Action/Func, this way the evaluation is done in method (and so the catching of the exceptions), you don't have to use find selector outside of the element map itself.
I think you are looking for something simple like
public bool ElementExists(By locator)
{
return Driver.FindElements(locator).Any();
}
You would call it like
if (ElementExists(By.Id("LoginButton")))
{
// do something
}
You can't pass in an element because in order to pass it in, you would have to locate it first which would not be possible (it would throw an exception) if it's not present.
If you are trying to check an existing element, you could do something like the below.
public bool ElementExists(IWebElement e)
{
try
{
bool b = e.Displayed;
return true;
}
catch (Exception)
{
return false;
}
}
I have a property which is stored as a string and is to be edited via a PropertyGrid It is kept as a set of comma separated values e.g ABC, DEF, GHI. My users could edit the string directly but the values come from a closed set of values. So it is easier and safer for them to pick from a list.
I have written a simple custom UITypeEditor based on the excellent answer here
I also reviewed this but I did not really understand it might relate to my problem. 8-(
I set up a simple form that allows the user to pick from a list and the picked values are added to value to be returned. My problem is that the value is not being returned. I wondered if this is something to do with the immutability of strings. However it is probably something simple and stupid that I am doing.
Here is my code for the type editor based on Marc's answer:
public class AirlineCodesEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
var svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
var codes = value as string;
if (svc != null && codes != null)
{
using (var form = new AirlineCodesEditorGUI())
{
form.Value = codes;
if (svc.ShowDialog(form) == DialogResult.OK)
{
codes = form.Value; // update object
}
}
}
return value; // can also replace the wrapper object here
}
}
The form is trivial for my test I just edited the textbox with some new values:
public partial class AirlineCodesEditorGUI : Form
{
public AirlineCodesEditorGUI()
{
InitializeComponent();
}
public string Value {
get
{
return airlineCodes.Text;
}
set
{
airlineCodes.Text = value;
}
}
private void OnCloseButtonClick(object sender, System.EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
}
Perhaps someone would put me out of my misery.
You just need to return the value from the form, like this:
if (svc.ShowDialog(form) == DialogResult.OK)
{
value = form.Value;
}
I'm trying to build simple utility with caliburn micro and wpf. This simple code:
public bool CanTransfer
{
get { return this.canTransfer; }
set
{
this.NotifyOfPropertyChange(() => this.CanTransfer);
this.canTransfer = value;
}
}
public void Transfer()
{
Task.Factory.StartNew(this.Process);
}
private void Process()
{
this.CanTransfer = false;
Thread.Sleep(5000);
this.CanTransfer = true;
}
Does the following - Transfer button remains disabled, but I'd expect it to be enabled 5 seconds after, what am I doing wrong?
Swap those lines in CanTransfer setter method - now you first say something has changed while it actually hasn't and then you change it:
public bool CanTransfer
{
get { return this.canTransfer; }
set
{
this.canTransfer = value;
this.NotifyOfPropertyChange(() => this.CanTransfer);
}
}
I usually use method like ChangeAndNotify<T> (the only annoyance is declaration of the field), the code can be seen here:
private bool _canTransfer;
public string CanTransfer
{
get { return _canTransfer; }
set
{
PropertyChanged.ChangeAndNotify(ref _canTransfer, value, () => CanTransfer);
}
}
You are missing a notifiation:
this.NotifyOfPropertyChange(() => this.CanTransfer);
I hope I got the syntax right, I am typing from memory.
This basically tells WPF "Hey something changed, go and look at the new value".
Is it possible to change the update URL to different location of an installed ClickOnce application? If so, how can I do that?
You mention in your comment that you wish to change it "on the client side". This is not possible. Your client app must be able to check for the update at the previous location which will then redirect it to the new location for the immediately next deployment.
See How to move a ClickOnce deployment.
Is it possible with a trick.
You can deploy it to the default publish location. (the application shouldn't check for updates).
Then copy your deployment to the customers server.
Just install your application on the client machines.
The field System.Deployment.Application.ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri contains the location and .application where the application is installed from. If you know that, then you can simple execute this url.
To check if there is an update, examine the .application file en grape the version.
this is my helper class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace MatemanSC.Utility
{
public class ClickOnceUtil
{
Version _UpdateVersion = null;
public string UpdateLocation
{
get
{
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri;
}
}
public Version AvailableVersion
{
get
{
if (_UpdateVersion == null)
{
_UpdateVersion = new Version("0.0.0.0");
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
using (XmlReader reader = XmlReader.Create(System.Deployment.Application.ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri))
{
//Keep reading until there are no more FieldRef elements
while (reader.ReadToFollowing("assemblyIdentity"))
{
//Extract the value of the Name attribute
string versie = reader.GetAttribute("version");
_UpdateVersion = new Version(versie);
}
}
}
}
return _UpdateVersion;
}
}
public bool UpdateAvailable
{
get
{
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion != AvailableVersion;
}
}
public string CurrentVersion
{
get
{
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
}
}
public void Update()
{
System.Diagnostics.Process.Start(System.Deployment.Application.ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri);
Environment.Exit(0);
}
public void CheckAndUpdate()
{
try
{
if (UpdateAvailable)
Update();
}
catch (Exception)
{
}
}
}
}
And this how to use it:
public partial class App : Application
{
public App()
{
ClickOnceUtil clickonceutil = new ClickOnceUtil();
clickonceutil.CheckAndUpdate();
}
}
When you want to change the url that you will use to upgrade programs, you can just use url rewrite at web.config: the old program will point to the old url, but it will bring the new program, which will have the new url.