RonaldWidha

percaya sama occam razor
See also: Other Geeks@INDC

Nge-automate user testing pake WatiN di multiple browser: contoh fluent interfacing

Saya sudah memakai WatiN beberapa kali dan pernah ngeshare tips buat ngejalanin WatiN di firefox dengan Firefox plugin (which constantly crash even with the newer jssh-WINNT-3.x.xpi).

Kali ini aku pengen ngebagi code snippet buat make WatiN di semua browser tanpa perlu make browserFactory atau apalagi copy paste. Bagi yang ga tau WatiN apaan, mungkin bisa ngeliat contoh di bawah ini sebagai contoh Fluent Interfacing alias method yang dibikin supaya kalo nyambung-nyambung mudah dibaca

Ini targetnya:

[TestFixtureSetUp]
public void Context()
{
  CrossBrowserTestExecutor.Execute( Scenario )
    .WithAllBrowsers
    .Go();
}

public void Scenario(Browser browser)
{
  browser.GoTo( "http://www.google.com" );
  browser.Close()
}

Mayan bersih kan? Notice kalimatnya jadi mudah dibaca dibanding kalo ditaroh di baris yang berbeda.

So this is the code that enables you to do that:

public class CrossBrowserTestExecutor
{
  public static CrossBrowserTest Execute( Action<Browser> test )
  {
    return new CrossBrowserTest( test );
  }
}

public class CrossBrowserTest
{
  public CrossBrowserTest( Action<Browser> test )
  {
    TheTest = test;
    BrowserAgents = new List<Browser>();
  }

  public Action<Browser> TheTest { get; set; }

  public IList<Browser> BrowserAgents { get; set; }

  public CrossBrowserTest WithAllBrowsers
  {
    get
    {
      BrowserAgents.Add( new IE() );
      BrowserAgents.Add( new FireFox() );
      return this;
    }
  }

  public void Go()
  {
    foreach ( Browser agent in BrowserAgents )
    {
      TheTest( agent );
    }
  }
}

Feel free to use it in any of your project, but please drop a comment if you find it useful. Thanks

Share this post: | | | |

Comments

No Comments