github.com/bazelbuild/rules_webtesting@v0.2.0/README.md (about) 1 # Bazel Web Testing Rules 2 3 [](https://ci.bazel.io/job/rules_webtesting) 5 6 Bazel rules and supporting code to allow testing against a browser with 7 WebDriver. 8 9 ## Configure your Bazel project 10 11 Add the following to your WORKSPACE file: 12 13 ```bzl 14 # Load rules_go at master for example purposes only. You should specify 15 # a specific version in your project. 16 http_archive( 17 name = "io_bazel_rules_go", 18 strip_prefix = "rules_go-master", 19 urls = [ 20 "https://github.com/bazelbuild/rules_go/archive/master.tar.gz", 21 ], 22 ) 23 load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") 24 go_rules_dependencies() 25 go_register_toolchains() 26 27 # Load rules_webtesting at master for example purposes only. You should specify 28 # a specific version in your project. 29 http_archive( 30 name = "io_bazel_rules_webtesting", 31 strip_prefix = "rules_webtesting-master", 32 urls = [ 33 "https://github.com/bazelbuild/rules_webtesting/archive/master.tar.gz", 34 ], 35 ) 36 37 load("@io_bazel_rules_webtesting//web:repositories.bzl", 38 "browser_repositories", 39 "web_test_repositories") 40 41 web_test_repositories() 42 43 # Load repositories for example browser definitions. 44 # You should create your own browser definitions and link 45 # to the specific browser versions you are interested in 46 # testing with. 47 browser_repositories( 48 chromium = True, 49 firefox = True, 50 ) 51 ``` 52 53 ## Write your tests 54 55 Write your test in the language of your choice, but use our provided Browser API 56 to get an instance of WebDriver. 57 58 Example Test (Java): 59 60 ```java 61 import com.google.testing.web.WebTest; 62 import org.junit.Test; 63 import org.junit.runner.RunWith; 64 import org.junit.runners.JUnit4; 65 import org.openqa.selenium.WebDriver; 66 67 @RunWith(JUnit4.class) 68 public class BrowserTest { 69 private WebDriver driver; 70 71 @Before public void createDriver() { 72 driver = new WebTest().newWebDriverSession(); 73 } 74 75 @After public void quitDriver() { 76 try { 77 driver.quit(); 78 } finally { 79 driver = null; 80 } 81 } 82 83 // your tests here 84 } 85 ``` 86 87 Example Test (Go): 88 89 ```go 90 import ( 91 "testing" 92 93 "github.com/tebeka/selenium" 94 "github.com/bazelbuild/rules_webtesting/go/webtest" 95 ) 96 97 func TestWebApp(t *testing.T) { 98 wd, err := webtest.NewWebDriverSession(selenium.Capabilities{}) 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 // your test here 104 105 if err := wd.Quit(); err != nil { 106 t.Logf("Error quitting webdriver: %v", err) 107 } 108 } 109 ``` 110 111 Example Test (Python): 112 113 ```python 114 import unittest 115 from testing.web import webtest 116 117 118 class BrowserTest(unittest.TestCase): 119 def setUp(self): 120 self.driver = webtest.new_webdriver_session() 121 122 def tearDown(self): 123 try: 124 self.driver.quit() 125 finally: 126 self.driver = None 127 128 # Your tests here 129 130 if __name__ == "__main__": 131 unittest.main() 132 ``` 133 134 In your BUILD files, load the correct language specific build rule and create a 135 test target using it: 136 137 ```bzl 138 load("@io_bazel_rules_webtesting//web:py.bzl", "py_web_test_suite") 139 140 py_web_test_suite( 141 name = "browser_test", 142 srcs = ["browser_test.py"], 143 browsers = [ 144 # For experimental purposes only. Eventually you should 145 # create your own browser definitions. 146 "@io_bazel_rules_webtesting//browsers:chromium-native", 147 ], 148 local = True, 149 deps = ["@io_bazel_rules_webtesting//testing/web"], 150 ) 151 ```