0

I have an APP that is in english and in spanish. I have automated some flows, using english language, so the asserts are in english. Now, I want to automate it in spanish, so the only thing I need to do is to add asserts in spanish.

How can I add spanish asserts and check IF one OR other = true THEN ok, in order to be able reuse easily all the code I have?

This how I use the asserts:

if (_app.Query(x => x.Class("SystemWebView").Css("BUTTON#button_home_back")).Length > 0)
{
    _app.WaitForElement(e => e.Css("BUTTON#button_home_back"), "Timeout waiting for the Home screen", new TimeSpan(0, 1, 0), null, null);

    Assert.AreEqual(_app.Query(e => e.Css("ion-title"))[0].TextContent, "englishString");                                        
     _app.Tap(x => x.Class("SystemWebView").Css("BUTTON#button_home_back"));
}

_app.Screenshot("englishString");
break;
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • *"spanish asserts"* - can you give an example of what you mean? Do you want to localize [Assert](https://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert(v=vs.110).aspx) messages? – Sinatr May 23 '18 at 11:42
  • Just change the language of the string used in the assert: Assert.AreEqual(_app.Query(e => e.Css("ion-title"))[0].TextContent, "spanishString") – Ivan Garrido May 23 '18 at 11:44

2 Answers2

3

If i were to do that, i would use dictionary and enum. something like this

enum Lang { English, Spanish }

Lang CurrentLang = Lang.English;

Dictionary<Lang, string> Asserts = new Dictionary<Lang, string>{
    {Lang.English, "englishString"},
    {Lang.Spanish, "spanishString"}
};

Then

Assert.AreEqual(_app.Query(e => e.Css("ion-title"))[0].TextContent, Asserts[CurrentLang]); 
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • looks well but, this works if we have more asserts on the test? – Ivan Garrido May 23 '18 at 11:59
  • You probably need to use resx file. https://stackoverflow.com/questions/1142802/how-to-use-localization-in-c-sharp @IvanGarrido – M.kazem Akhgary May 23 '18 at 12:01
  • or you could use a class instead of string that holds multiple string properties for different asserts. for example `Dictionary` and then `Asserts[CurrentLang].AssertString_1`, but its probably not the best way. – M.kazem Akhgary May 23 '18 at 12:04
2

You could create a method that returns the correct string based on the language.

Assert.AreEqual(_app.Query(e => e.Css("ion-title"))[0].TextContent, 
    GetTitleInCurrentLanguage()); 

By the time you do this assert you must already know in which language the app is running so you should definitely do the assert against the exact string for that language, not in a a || b fashion, because you want the assert to fail in case there is a Spanish title when the app should be in English.

The way you implement the method that returns localized resources depends purely on the way localization is built in your app. In case of test you may want to hardcode the strings in tests instead to make sure you are not depending on the localization infrastructure to be functional. Or you can separately run tests to make sure this infrastructure works.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Really? Does that need an answer? – Sinatr May 23 '18 at 11:45
  • 4
    It is a question so... probably :-) ? – Martin Zikmund May 23 '18 at 11:47
  • I wasn't specific on purpose as I didn't know if the goal is to test localization itself or just the fact that there is the correct string on the button. And also because of the fact that I don't know which localization mechanism is OP using :-) – Martin Zikmund May 23 '18 at 12:04
  • 1
    I am convinced by upvotes, seems your answer is useful. To me converting a constant expression into a method call to return different result based on some condition is too obvious.. I was expecting harder problem (question is unclear). – Sinatr May 23 '18 at 12:16
  • @Sinatr: if the user had 100k rep then I might be inclined to agree with you but this site has users of all ability and while the low rep doesn't necessarily mean low knowledge it can do. If an answer is simple that's not a problem as long as it answers the question. The OP can worry about if it is suitable for their needs (and comment if they want). – Chris May 23 '18 at 13:18
  • My knowledge of C# is nearly 0, so I dont´t know how to do what I want. My idea is the following: make a "library" or resource for each language, inside then I will define the same variables but with the value in different language. Then, depending of the language of the app, I will use one resource or the other, so the string of the assert will change dinamically. The problem is that I dont know how to do this :( I think that is so easy to do, but as I said, I have no idea of C# – Ivan Garrido May 23 '18 at 13:24