Using isEnabled() for a test control - webdriver

I am using PageObject model in developing test scripts. I am trying to put into test code control of a current situation. In my case it is an element enabled or not. The source code below.
#FindBy(linkText="Overview")
private WebElement overviewTab;
public boolean checkOverviewTabVisable() {
return overviewTab.isEnabled();
}
if(customerSitePage.checkOverviewTabVisable()) {
customerSitePage.getOverviewTab();
} else {
sitesTabsList.getBuyerSite();
customerSitePage.getOverviewTab();
}
Script hangs in the line of checkOverviewTabVisable() method call and no any error. Something weird in isEnabled() behavior. Has anyone used this method for test control?

Related

MSTest - Integration tests - use value of another test

I know that unit tests should run isolated and should never depend on other unit tests.
However, I also write some integration tests with MSTest and sometimes they produce a result that I would like to reuse in another test.
For example:
Creating a user
Searching this user from the database
Deleting the user
Each of those points would be an integration test for me, so I would like to write methods that look like this:
User _myNewUser;
[TestMethod]
public void CreateAUserTest()
{
//User gets created here somehow....
_myNewUser = successfullyCreatedUser;
}
And this test should run after the preceeding test:
User _myNewUser;
[TestMethod]
public void SearchingUserTest()
{
var user = searchUser(_newUser.GetName());
//Assert that user is not null
}
You can see that I use the value of the first test in the second test.
With a playlist I could make sure that both tests run in the correct order.
However, in VS 2022 each test gets executed in isolation, so what I am trying to do does not work.
_newUser is always null if I run the second test, even if the first test was a success.
Is my idea bad in general?
If not: How can I use the produced data of a test in another test?
I usually extract the contents of a test like that into a separate staging function that does not contain the [TestMethod] attribute, so that I can reuse it to stage other tests.
private void Stage_CreateAUser()
{
//do work from CreateAUserTest()
}
[TestMethod]
public void CreateAUserTest()
{
Stage_CreateAUser();
}
private void Stage_SearchingUser()
{
//do work from CreateAUserTest()
}
[TestMethod]
public void SearchingUserTest()
{
Stage_CreateAUser();
Stage_SearchingUser();
}
Etc...

Cannot initialize ChromiumWebBrowser inside a JSB-bounded class

I have followed this steps to setup JSB. In my bounded-class, I need to return the source of an API request (I cannot, therefore, use GetPageSourceAsync because no JS context is ever created). So, I use the trick of copying and immediately pasting the page content to a string variable (see result). This code is not working, I get the following exception:
The ChromiumWebBrowser instance creates the underlying Chromium Embedded Framework (CEF) browser instance in an async fashion. The undelying CefBrowser instance is not yet initialized. Use the IsBrowserInitializedChanged event and check the IsBrowserInitialized property to determine when the browser has been initialized.
which is happening on bro.SelectAll();:
public class boundClass
{
ChromiumWebBrowser bro;
public boundClass()
{
bro = new ChromiumWebBrowser("https://google.com");
}
public string getAuthSource(string url)
{
string source = String.Empty;
bro.Load(url);
while (bro.IsLoading)
{
}
bro.SelectAll();
bro.Copy();
string result = Clipboard.GetText();
Clipboard.Clear();
return result;
}
}
What's the problem? The ChromiumWebBrowser seems well-initialized to me...

Unit testing if methods find and return a view in ASP.NET

I'm new to ASP.NET and I'm refactoring some functionalities in my MVC-structured ASP.NET application into area's. This has already lead to controller-methods not able to find their views anymore, which results in the following page:
To test if all controllers can find their views, I'd like to write some automated unit tests for this.
I have came up with the following:
[TestMethod]
public void AboutTest()
{
var controller = new HomeController();
var result = controller.About() as ViewResult;
Assert.IsNotNull(result);
}
which tests the About-method in the following code:
public class HomeController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View("~/Views/SomeFolder/Contact.cshtml");
}
}
But even when the HomeControllers About-method can not find a view, this assert succeeds, so this does not work for me.
I have found a solution online to use use ViewEngine.FindView() here. I don't think I can use this, since in some controllers the views are referenced by a hardcoded string (see the contact method in the example controller above) instead of just returning the default view (simularly named as its method). The ViewEngine.FindView(controller.ControllerContext, "about", "about"); will then fail, but the controller-method would not.
Another solution states to use Assert.IsEqual() and check if the result.ViewName is equal to a hardcoded string (for example: "About"). Since I do not set or know the title of the views I'm expecting to get returned, this would not be a solution either.
(How) would I be able to test my application for this?
You shouldn't check for null, it will return a ViewResult even when it doesn't render.
To test whether it actually renders use AssertViewRendered from mvccontrib.
[TestMethod]
public void AboutTest()
{
var controller = new HomeController();
var result = controller.About().AssertViewRendered();
}
You can even check for a specific view like so:
result.AssertViewRendered().ForView(MVC.Your.Views.AboutView);
Or supply data like so:
controller.page().AssertViewRendered().ForView("page").WithViewData<SomeModel>();
For an interactive tutorial with lots of pictures I can recommend: http://toreaurstad.blogspot.nl/2011/09/adventures-with-mvccontrib-testhelper.html
Edit:
You might also check out Selenium to test your entire app (incl. rendering of 200 routes).

phpunit custom teardown specific to my test

I have some setup specific to my test in a class. Since it is specific to my test, I have added to it to the top of my test function. The cleanup is added to the end of the test function. The problem when the test fails and the cleanup does not gets executed. Is there a PHPUnit way to specify a custom teardown specific to my test function. I looked at the PHPUnit manual which specifies teardownAfterClass and tearDown, but both does not solve my problem. The function teardownAfterClass will run only once at the end of the class. The function teardown runs after each test, but I do not want to do any cleanup if my specific test function was not executed.
What is the PHPUnit way of creating custom teardown function for my test?
Here is the code which I use to make sure that the cleanup specific to the test always happen, but it is ugly as it needs to put the actual test in a separate function and needs try/catch block. Is there is a PHPUnit specific way to handle it? Something like dataProvider specific to function will be great, which always gets executed after the test irrespective of failure or success.
class testClass {
public function test1() {
self::setupSpecificToTest1();
try {
// actual test without cleanup
$this->_test1();
} catch (Exception $e) {
self::cleanupSpecificToTest1();
throw $e;
}
self::cleanupSpecificToTest1();
}
public function test2() {
// some code which does not need any setup or cleanup
}
private function _test1() {
// some test code
}
}
Implement intelligent tearDown (it's closer to PHPUnit's approach to running tests)
You can check for the specific test name inside tearDown method to alternate its behaviour accordingly. Getting test name can be done via $this->getName() in the test class.
Try something like:
...
public function test1() { ... }
public function test2() { ... }
public function tearDown()
{
if ($this->getName() === 'test1')
{
// Do clean up specific to test1
}
parent::tearDown();
}
I have tried this, it worked for me after my test.
public function tearDown()
{
$this->webDriver->close();
}

How to handle wait during a datadriven test using testNG in a webdriver script

This is my scenario, i use webdriver with testNG for doing data driven test. I am observing that the data i am 'seeing' in web app which is provided by #dataprovider is missing some value. For exg if i have an array as {"1","2","3","4","5"}, i am getting these value in webdriver script using testNG #dataprovider, i am observing in the 'Web GUI' initially 2 might be displayed, then in the next iteration 5 is displayed then the test stop.
I am assuming that TestNG is not waiting for webdriver to complete the function or process.
Here is my sample code
#Test (dataProviderClass=MyDataProviders.class)
public class MyWebDriverClass{
#Test(dataProvider = "theProviderName")
public void providerHomeCreateuser(String arg1,String arg2)
{
<..input arg1, arg2 to text fields..>
}
}
I understand somewhere i need to put a Thread.wait(), could any body guide me on this.
Data provider method is as follows
public class MyDataProviders {
...
...
#DataProvider (name="theProviderName")
public static Object[][] getData() throws Exception
{
Object retObject[][]=getTableArray("src\\com\\abcd\\resource\\TestData.xls", 5, "MyTestData");
return retObject;
}

Resources