Cosmological test of gravity with gravitational waves